How do I shuffle a list of dictionaries?

Vale

Quick question, I'm trying to display random questions in a simple quiz program using a list of dictionaries. I found a way to do this, but it was fairly drawn out and buggy so I decided it would be much easier to iterate through the questions instead after shuffling them once at the start.

I stumbled across random.shuffle() which seems to be just what I want, however, I can't seem to get it to work. Here's what I'm currently trying:

import random

quizBank = {
    1:    ["What is 1+1?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n",'A'],
    2:    ["What is 2+2?\nA) 2\nB) 4\nC) 1\nD) None of the above.\n\n",'B'],
    3:    ["What is 3+3?\nA) 2\nB) 11\nC) 6\nD) None of the above.\n\n",'C'],
    4:    ["What is 4+4?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n",'D'],
    5:    ["What is 5+5?\nA) 2\nB) 11\nC) 10\nD) None of the above.\n\n",'C'],
    6:    ["What is 6+6?\nA) 2\nB) 12\nC) 1\nD) None of the above.\n\n",'B'],
    }

random.shuffle(quizBank)
print(quizBank)

Which results in the following error:

Traceback (most recent call last):
  File "C:\Users\MyUserName\Desktop\test.py", line 12, in <module>
    random.shuffle(quizBank)
  File "C:\Python27\lib\random.py", line 291, in shuffle
    x[i], x[j] = x[j], x[i]
KeyError: 0

Does anyone know what I am doing wrong and can give me a point in the right direction?

tdelaney

The answer I already gave you is good. You could also shuffle the values:

values = quizBank.values()
random.shuffle(values)

One of the reasons that I suggested not using a dict is that once you shuffle, the indexes you setup are shuffled too.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I shuffle a multidimensional list in Python

From Dev

How do I create a list of dictionaries?

From Dev

How do I shuffle a deque?

From Java

How do I sort a list of dictionaries by a value of the dictionary?

From Dev

How do I sort list of numerical value dictionaries?

From Dev

How do I compare multiple key values from a list of dictionaries?

From Dev

How do I compare a list of dictionaries for equality in C#

From Dev

How do I add keys to a dictionary in a list of dictionaries?

From Dev

How do I format a list of dictionaries with itertools.groupby?

From Dev

How do I compare multiple key values from a list of dictionaries?

From Java

How do I shuffle an array in Swift?

From Dev

How do I shuffle the order of an array in Jekyll?

From Dev

How can I update a list of dictionaries here?

From Dev

How can I save a list of dictionaries to a file?

From Dev

How to I parse JSON list of dictionaries in javascript

From Dev

In Python how do I create a data frame with a list that contains multiple levels of dictionaries?

From Dev

How do I extract all the values of a specific key from a list of dictionaries?

From Dev

Python: How do I get a list of all keys in a dictionary of dictionaries, at a given depth

From Dev

How do I filter DataFrame rows based on a key value pair from a list of dictionaries column field?

From Dev

How do I delete key/value pair in list of dictionaries pandas env

From Dev

if id is given, how do i find the other values of other attributes in this list with dictionaries inside

From Dev

In Python how do I create a data frame with a list that contains multiple levels of dictionaries?

From Dev

How do I calculate the percent difference between the 1st and 6th key in a list of dictionaries?

From Dev

How do I remove the "u" prefix from a (json serialized) list of dictionaries?

From Dev

How do I make a combobox display a list of dictionaries and act upon the selected value?

From Dev

In Ansible, how do I setup dictionaries with lists?

From Java

How do I compare two dictionaries in Swift?

From Dev

How do I convert lists and dictionaries into bytes?

From Dev

How to shuffle a ImmutableJS List

Related Related

  1. 1

    How do I shuffle a multidimensional list in Python

  2. 2

    How do I create a list of dictionaries?

  3. 3

    How do I shuffle a deque?

  4. 4

    How do I sort a list of dictionaries by a value of the dictionary?

  5. 5

    How do I sort list of numerical value dictionaries?

  6. 6

    How do I compare multiple key values from a list of dictionaries?

  7. 7

    How do I compare a list of dictionaries for equality in C#

  8. 8

    How do I add keys to a dictionary in a list of dictionaries?

  9. 9

    How do I format a list of dictionaries with itertools.groupby?

  10. 10

    How do I compare multiple key values from a list of dictionaries?

  11. 11

    How do I shuffle an array in Swift?

  12. 12

    How do I shuffle the order of an array in Jekyll?

  13. 13

    How can I update a list of dictionaries here?

  14. 14

    How can I save a list of dictionaries to a file?

  15. 15

    How to I parse JSON list of dictionaries in javascript

  16. 16

    In Python how do I create a data frame with a list that contains multiple levels of dictionaries?

  17. 17

    How do I extract all the values of a specific key from a list of dictionaries?

  18. 18

    Python: How do I get a list of all keys in a dictionary of dictionaries, at a given depth

  19. 19

    How do I filter DataFrame rows based on a key value pair from a list of dictionaries column field?

  20. 20

    How do I delete key/value pair in list of dictionaries pandas env

  21. 21

    if id is given, how do i find the other values of other attributes in this list with dictionaries inside

  22. 22

    In Python how do I create a data frame with a list that contains multiple levels of dictionaries?

  23. 23

    How do I calculate the percent difference between the 1st and 6th key in a list of dictionaries?

  24. 24

    How do I remove the "u" prefix from a (json serialized) list of dictionaries?

  25. 25

    How do I make a combobox display a list of dictionaries and act upon the selected value?

  26. 26

    In Ansible, how do I setup dictionaries with lists?

  27. 27

    How do I compare two dictionaries in Swift?

  28. 28

    How do I convert lists and dictionaries into bytes?

  29. 29

    How to shuffle a ImmutableJS List

HotTag

Archive