3

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?

Copperfield
  • 8,131
  • 3
  • 23
  • 29
jay a
  • 624
  • 2
  • 7
  • 11
  • 2
    `dict.values()` will give you the values as a list (more or less) – Stephen Rauch Jan 28 '17 at 22:29
  • 3
    Possible duplicate of [Python: simplest way to get list of values from dict?](http://stackoverflow.com/questions/16228248/python-simplest-way-to-get-list-of-values-from-dict) – Stephen Rauch Jan 28 '17 at 22:30

5 Answers5

5

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)
>>> 
Copperfield
  • 8,131
  • 3
  • 23
  • 29
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()
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
1
>>> a = {0:0, 1:2, 2:4}
>>> a.keys()
[0, 1, 2]
>>> a.values()
[0, 2, 4]
Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
0

Here is my one-liner solution for a flattened list of keys and values:

d = {'foo': 'bar', 'zoo': 'bee'}
list(sum(d.items(), tuple()))

And the result:

['foo', 'bar', 'zoo', 'bee'] 
MgAl2O4
  • 31
  • 3
-1

A dictionary is defined as the following:

dict{[Any]:[Any]} = {[Key]:[Value]}

The problem with your question is that you haven't clarified what the keys are.

1: Assuming the keys are just numbers and in ascending order without gaps, dict.values() will suffice, as other authors have already pointed out.

2: Assuming the keys are just numbers in strictly ascending order but not in the right order:

i = 0
list = []
while i < max(mydict.keys()):
     list.append(mydict[i])
     i += 1

3: Assuming the keys are just numbers but not in strictly ascending order:
There still is a way, but you have to get the keys first and do it via the maximum of the keys and an try-except block

4: If none of these is the case, maybe dict is not what you are looking for and a 2d or 3d array would suffice? This also counts if one of the solutions do work. Dict seems to be a bad choice for what you are doing.

Narusan
  • 482
  • 1
  • 5
  • 18
  • `i++` is not valid python – Copperfield Jan 29 '17 at 20:36
  • 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 – Copperfield Jan 29 '17 at 21:33
  • 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? – Copperfield Jan 29 '17 at 21:54
  • 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 Jan 29 '17 at 21:58
  • I wasn't quite sure what the questioner wanted, that's why I provided them with 4 options – Narusan Jan 29 '17 at 22:08