Python And No Obvious Way To Get A Specific Element From A Dictionary
Solution 1:
You access a specific element in a dictionary by key. That's what a dictionary is. If that behavior isn't what you want, use something other than a dictionary.
a dict is supposed to be unordered but then why does the for-loop print them in the order I put them in?
Coincidence: basically, you happened to put them in in the order that Python prefers. This isn't too hard to do, especially with integers (int
s are their own hashes and will tend to come out from a dict in ascending numeric order, though this is an implementation detail of CPython and may not be true in other Python implementations), and especially if you put them in in numerical order to begin with.
"Unordered" really means that you don't control the order, and it may change due to various implementation-specific criteria, so you should not rely on it. Of course when you iterate over a dictionary elements come out in some order.
If you need to be able to access dictionary elements by numeric index, there are lots of ways to do that. collections.OrderedDict
is the standard way; the keys are always returned in the order you added them, so you can always do foo[foo.keys()[i]]
to access the i
th element. There are other schemes you could use as well.
Solution 2:
Python dicts are accessed by hashing the key. So if you have any sort of a sizable dict and things are coming out in the order you put them in, then it's time to start betting on the lottery!
my_dict = {}
my_dict['a'] = 1
my_dict['b'] = 2
my_dict['c'] = 3
my_dict['d'] = 4
for k,v in my_dict.items():
print k, v
yields:
a1
c 3b2
d 4
Solution 3:
d = {}
d['first'] = 1
d['second'] = 2
d['third'] = 3
print d
# prints {'seconds': 2, 'third': 3, 'first': 1}# Hmm, just like the docs say, order of insertion isn't preserved.print d['third']
# prints 3# And there you have it: access to a specific element
Solution 4:
If you want to iterate through the items in insertion order, you should be using OrderedDict. A regular dict is not guaranteed to do the same, so you're asking for trouble later if you rely on it to do so.
If you want to access a particular item, you should access it by its key using the []
operator or the get()
method. That's the primary function of a dict, after all.
Solution 5:
Result of the hashing of several values varies according the values:
sometimes the order seems to be kept: following example with d_one
generaly, the order is not kept: following example with d_two
Believing that the order is anyway kept is only because you are deceived by particuliar cases in which the order is apparently conserved
d_one = {}
for i,x inenumerate((122,'xo','roto',885)):
print x
d_one[x] = i
printfor k in d_one:
print k
print'\n=======================\n'
d_two = {}
for i,x inenumerate((122,'xo','roto','taratata',885)):
print x
d_two[x] = i
printfor k in d_two:
print k
result
122
xo
roto
885
122
xo
roto
885
=======================
122
xo
roto
taratata
885
122
taratata
xo
roto
885
By the way, what you call "elements of a dictionary' are commonly called 'items of the dictionary' ( hence the methods items() and iteritems() of a dictionary)
Post a Comment for "Python And No Obvious Way To Get A Specific Element From A Dictionary"