Python access first element in dictionary
Access the first element in a python dict in a non-destructive fashion.
from collections import OrderedDict
d = OrderedDict([
('a', 1),
('b', 2),
('c', 3),
])
key = dict.iterkeys().next()
print(key) # "a"
Note: Be careful with unordered dictionaries.
dict = {'a': 1, 'b': 2, 'c': 3}
key = dict.iterkeys().next()
print(key) # Not always "a"
Comments
Leave a Reply