How do you convert a Dictionary to a List?

13,307

Solution 1

convert a dictionary to a list is pretty simple, you have 3 flavors for that .keys(), .values() and .items()

>>> test = {1:30,2:20,3:10}
>>> test.keys() # you get the same result with list(test)
[1, 2, 3]
>>> test.values()
[30, 20, 10]
>>> test.items()
[(1, 30), (2, 20), (3, 10)]
>>> 

(in python 3 you would need to call list on those)

finding the maximum or minimum is also easy with the min or max function

>>> min(test.keys()) # is the same as min(test)
1
>>> min(test.values())
10
>>> min(test.items())
(1, 30)
>>> max(test.keys()) # is the same as max(test)
3
>>> max(test.values())
30
>>> max(test.items())
(3, 10)
>>>     

(in python 2, to be efficient, use the .iter* versions of those instead )

the most interesting one is finding the key of min/max value, and min/max got that cover too

>>> max(test.items(),key=lambda x: x[-1])
(1, 30)
>>> min(test.items(),key=lambda x: x[-1])
(3, 10)
>>>     

here you need a key function, which is a function that take one of whatever you give to the main function and return the element(s) (you can also transform it to something else too) for which you wish to compare them.

lambda is a way to define anonymous functions, which save you the need of doing this

>>> def last(x):
        return x[-1] 

>>> min(test.items(),key=last)
(3, 10)
>>> 

Solution 2

You can simply take the minimum with:

min(dic.values())

And convert it to a list with:

list(dic.values())

but since a dictionary is unordered, the order of elements of the resulting list is undefined.

In you do not need to call list(..) simply dic.values() will be sufficient:

dic.values()

Solution 3

>>> a = {0:0, 1:2, 2:4}
>>> a.keys()
[0, 1, 2]
>>> a.values()
[0, 2, 4]
Share:
13,307
jay a
Author by

jay a

Updated on June 04, 2022

Comments

  • jay a
    jay a almost 2 years

    For example, if the Dictionary is {0:0, 1:0, 2:0} making a list: [0, 0, 0].

    If this isn't possible, how do you take the minimum of a dictionary, meaning the dictionary: {0:3, 1:2, 2:1} returning 1?

  • Tasos Papastylianou
    Tasos Papastylianou over 7 years
    yeah, saw that a second later :p
  • Tasos Papastylianou
    Tasos Papastylianou over 7 years
    you are quick! :D
  • Copperfield
    Copperfield over 7 years
    i++ is not valid python
  • Copperfield
    Copperfield over 7 years
    also, there is no need to assume anything about the keys (or values) other than they can be compared with one another to find min/max, you go to some weird tangent there
  • Narusan
    Narusan over 7 years
    .items() is genius. I didn't know such a thing existed. Thanks!
  • Copperfield
    Copperfield over 7 years
    in your example 2, you assume that that the keys are numbers for, actually I don't understand your goal there :-/ In your point 1, why you would assume that? likewise in point 3, and what is the point of point 4?
  • Narusan
    Narusan over 7 years
    The point of point 4? I wasn't sure what he wanted, but if the keys are numbers, one could simply use an array with the index correlating to the key. {0:1, 3:2, 2:3} would mean that the list looks like [1, 3, 2].
  • Narusan
    Narusan over 7 years
    I wasn't quite sure what the questioner wanted, that's why I provided them with 4 options