On top of the already provided answers there is a very nice pattern in Python that allows you to enumerate both keys and values of a dictionary.
The normal case you enumerate the keys of the dictionary:
example_dict = {1:'a', 2:'b', 3:'c', 4:'d'}
for i, k in enumerate(example_dict):
print(i, k)
Which outputs:
0 1
1 2
2 3
3 4
But if you want to enumerate through both keys and values this is the way:
for i, (k, v) in enumerate(example_dict.items()):
print(i, k, v)
Which outputs:
0 1 a
1 2 b
2 3 c
3 4 d
Answer from João Almeida on Stack Overflowenumerate() for dictionary in Python - Stack Overflow
python - How to iterate `dict` with `enumerate` and unpack the index, key, and value along with iteration - Stack Overflow
how to loop through dict with enumerate and unpack the index, key, and value
python - How to iterate over dictionary using 'enumerate' without assigning loop count to 'key'? - Stack Overflow
On top of the already provided answers there is a very nice pattern in Python that allows you to enumerate both keys and values of a dictionary.
The normal case you enumerate the keys of the dictionary:
example_dict = {1:'a', 2:'b', 3:'c', 4:'d'}
for i, k in enumerate(example_dict):
print(i, k)
Which outputs:
0 1
1 2
2 3
3 4
But if you want to enumerate through both keys and values this is the way:
for i, (k, v) in enumerate(example_dict.items()):
print(i, k, v)
Which outputs:
0 1 a
1 2 b
2 3 c
3 4 d
The first column of output is the index of each item in enumm and the second one is its keys. If you want to iterate your dictionary then use .items():
for k, v in enumm.items():
print(k, v)
And the output should look like:
0 1
1 2
2 3
4 4
5 5
6 6
7 7
Instead of using mydict, you should be using mydict.items() with enumerate as:
for i, (k, v) in enumerate(mydict.items()):
# your stuff
Sample example:
mydict = {1: 'a', 2: 'b'}
for i, (k, v) in enumerate(mydict.items()):
print("index: {}, key: {}, value: {}".format(i, k, v))
# which will print:
# -----------------
# index: 0, key: 1, value: a
# index: 1, key: 2, value: b
Explanation:
enumerate()returns an iterator object which contains tuples in the format:[(index, list_element), ...]dict.items()returns an iterator object (in Python 3.x. It returnslistin Python 2.7) in the format:[(key, value), ...]- On combining together,
enumerate(dict.items())will return an iterator object containing tuples in the format:[(index, (key, value)), ...]
It's also possible to use itertools.count to count along.
from itertools import count
for i, (k, v) in zip(count(), mydict.items()):
# do something
This is useful especially if you want the counter to be fractional numbers. For example,
mydict = dict(zip(range(3), range(10, 13)))
for i, (k, v) in zip(count(step=0.5), mydict.items()):
print(f"index={i:<3}, key={k}, value={v}")
# index=0 , key=0, value=10
# index=0.5, key=1, value=11
# index=1.0, key=2, value=12
an example of the dict
{10: {'total_power_usage': 152.1, 'total_pris': 336.62499999999994, 'huse_usage': 74.45588194444376, 'hus_pris': 195.42238509638779, 'charge_usage': 77.64411805555619, 'charge_price_full': 141.1951349036122, 'charge_price': 63.55101684805601}}now i can do :
for i, (k, v) in enumerate(my_result.items()):
print("index: {}, key: {}, value: {}".format(i, k, v))and that prints each line in the dict just fine. But how do i access each named element in the dict? if i need the value from 'total_pris' or 'charge_usage' ?