TypeError: "range" object does not support element assignment

I looked at some python 2.x code and tried to translate it to py 3.x but I am stuck in this section. Can someone clarify what is wrong?

 import random emails = { "x": "[REDACTED]@hotmail.com", "x2": "[REDACTED]@hotmail.com", "x3": "[REDACTED]@hotmail.com" } people = emails.keys() #generate a number for everyone allocations = range(len(people)) random.shuffle(allocations) 

It was a mistake:

 TypeError: 'range' object does not support item assignment 
+26
source share
1 answer

In Python 3, range returns a lazy sequence object - it does not return a list. It is not possible to reorder items in a range object, so it cannot be shuffled.

Convert it to a list before shuffling.

 allocations = list(range(len(people))) 
+63
source

Source: https://habr.com/ru/post/1394127/


All Articles