In Python versions before 3.7 dictionaries were inherently unordered, so what you're asking to do doesn't really make sense.
If you really, really know what you're doing, use
Copyvalue_at_index = list(dic.values())[index]
Bear in mind that prior to Python 3.7 adding or removing an element can potentially change the index of every other element.
Answer from NPE on Stack OverflowIn Python versions before 3.7 dictionaries were inherently unordered, so what you're asking to do doesn't really make sense.
If you really, really know what you're doing, use
Copyvalue_at_index = list(dic.values())[index]
Bear in mind that prior to Python 3.7 adding or removing an element can potentially change the index of every other element.
Let us take an example of dictionary:
Copynumbers = {'first':0, 'second':1, 'third':3}
When I did
Copynumbers.values()[index]
I got an error:'dict_values' object does not support indexing
When I did
Copynumbers.itervalues()
to iterate and extract the values it is also giving an error:'dict' object has no attribute 'iteritems'
Hence I came up with new way of accessing dictionary elements by index just by converting them to tuples.
Copytuple(numbers.items())[key_index][value_index]
for example:
Copytuple(numbers.items())[0][0] gives 'first'
if u want to edit the values or sort the values the tuple object does not allow the item assignment. In this case you can use
Copylist(list(numbers.items())[index])
Get the index of the values in python dict
python - how to get access elements of a dictionary by its index? - Stack Overflow
python - How to get the index with the key in a dictionary? - Stack Overflow
python - How to index into a dictionary? - Stack Overflow
Videos
You need to convert the keys of the dictionary to a list in order to access by its index. This method has an O(n) complexity compared to the access by key values directly to the dictionary (hash table), which results in a constant O(1) time complexity:
Copya={'ali':10 , 'reza':19 , 'sara':15}
print(a[list(a.keys())[0]]) #Here you are getting a list of all keys of the dictionary by calling list(a.keys()), and accessing a specific element of that list with [:] operator
Output:
Copy10
Dictionaries aren't indexed, so no. They're associative, that is, you access elements by their keys. If you want an ordered collection, you need to use a list or a tuple.
Use OrderedDicts: http://docs.python.org/2/library/collections.html#collections.OrderedDict
>>> x = OrderedDict((("a", "1"), ("c", '3'), ("b", "2")))
>>> x["d"] = 4
>>> x.keys().index("d")
3
>>> x.keys().index("c")
1
For those using Python 3
>>> list(x.keys()).index("c")
1
Dictionaries in python (<3.6) have no order. You could use a list of tuples as your data structure instead.
d = { 'a': 10, 'b': 20, 'c': 30}
newd = [('a',10), ('b',20), ('c',30)]
Then this code could be used to find the locations of keys with a specific value
locations = [i for i, t in enumerate(newd) if t[0]=='b']
>>> [1]
If anybody is still looking at this question, the currently accepted answer is now outdated:
Since Python 3.7*, dictionaries are order-preserving, that is they now behave like collections.OrderedDicts. Unfortunately, there is still no dedicated method to index into keys() / values() of the dictionary, so getting the first key / value in the dictionary can be done as
Copyfirst_key = list(colors)[0]
first_val = list(colors.values())[0]
or alternatively (this avoids instantiating the keys view into a list):
Copydef get_first_key(dictionary):
for key in dictionary:
return key
raise IndexError
first_key = get_first_key(colors)
first_val = colors[first_key]
If you need an n-th key, then similarly
Copydef get_nth_key(dictionary, n=0):
if n < 0:
n += len(dictionary)
for i, key in enumerate(dictionary.keys()):
if i == n:
return key
raise IndexError("dictionary index out of range")
* CPython 3.6 already included insertion-ordered dicts, but this was only an implementation detail. The language specification includes insertion-ordered dicts from 3.7 onwards.
Dictionaries are unordered in Python versions up to and including Python 3.6. If you do not care about the order of the entries and want to access the keys or values by index anyway, you can create a list of keys for a dictionary d using keys = list(d), and then access keys in the list by index keys[i], and the associated values with d[keys[i]].
If you do care about the order of the entries, starting with Python 2.7 you can use collections.OrderedDict. Or use a list of pairs
Copyl = [("blue", "5"), ("red", "6"), ("yellow", "8")]
if you don't need access by key. (Why are your numbers strings by the way?)
In Python 3.7, normal dictionaries are ordered, so you don't need to use OrderedDict anymore (but you still can β it's basically the same type). The CPython implementation of Python 3.6 already included that change, but since it's not part of the language specification, you can't rely on it in Python 3.6.