Home > Software engineering >  Python- turn values in a dictionary into a list
Python- turn values in a dictionary into a list

Time:12-20

i have a question:

I have a dictionary like this

{1: [4, 2, 1, 3], 2: [4, 3, 1, 2], 3: [4, 3, 1, 2]}

and I want to get each of the values into their own list within a loop eg.

for each key in myDict
    myList = [4,2,1,3]

do something in my list/ clear my list then loop around so...

myList = [4,3,1,2]

I have no idea how to attempt this, does anybody have any suggestions?

CodePudding user response:

Dictionaries have a.values() method, and you can use it like so:

for myList in myDict.values():
    print(myList)  # Do stuff

Keep in mind camelCase isn't a convention in Python.

CodePudding user response:

solution of you problem

a={1: [4, 2, 1, 3], 2: [4, 3, 1, 2], 3: [4, 3, 1, 2]}

mylist=a[1]

print(mylist)

  • Related