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
?
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
?
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)
>>>
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 python-2.7 you do not need to call list(..)
simply dic.values()
will be sufficient:
dic.values()
>>> a = {0:0, 1:2, 2:4}
>>> a.keys()
[0, 1, 2]
>>> a.values()
[0, 2, 4]
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']
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.