def shufflemode():
import random
combined = zip(question, answer)
random.shuffle(combined)
question[:], answer[:] = zip(*combined)
but then i get the error: TypeError: object of type 'zip' has no len()
What do I do im so confused
def shufflemode():
import random
combined = zip(question, answer)
random.shuffle(combined)
question[:], answer[:] = zip(*combined)
but then i get the error: TypeError: object of type 'zip' has no len()
What do I do im so confused
I wonder the same thing. According to: randomizing two lists and maintaining order in python You should be able to do it like the OP tried, but i also get the same error. I think the ones from the link are using python 2 and not 3, could this be the problem?
This is an issue between Python 2 and Python 3. In Python 2 using shuffle after zip works, because zip returns a list. In Python 3: "TypeError: object of type 'zip' has no len()" because zip returns an iterator in Python 3.
Solution, use list() to convert to a list:
combined = list(zip(question, answer))
random.shuffle(combined)
The error appeared with shuffle() because shuffle() uses len().
References issue: The zip() function in python 3
Stumbled upon this and was surprised to learn about the random.shuffle method. So I tried your example, and it worked for me in Python 2.7.5:
def shufflemode():
import random
combined = zip(question, answer)
random.shuffle(combined)
question[:], answer[:] = zip(*combined)
question = ["q1","q2","q3","q4","q5"]
answer = ["a1","a2","a3","a4","a5"]
if __name__ == "__main__":
shufflemode()
print question,answer
The result is two lists with the same randomized sequence of questions and answers*strong text*:
>>>
['q3', 'q2', 'q5', 'q4', 'q1'] ['a3', 'a2', 'a5', 'a4', 'a1']
>>>