You could just iterate over the indices of the range of the len of your list:

dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
for index in range(len(dataList)):
    for key in dataList[index]:
        print(dataList[index][key])

or you could use a while loop with an index counter:

dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
index = 0
while index < len(dataList):
    for key in dataList[index]:
        print(dataList[index][key])
    index += 1

you could even just iterate over the elements in the list directly:

dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
for dic in dataList:
    for key in dic:
        print(dic[key])

It could be even without any lookups by just iterating over the values of the dictionaries:

dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
for dic in dataList:
    for val in dic.values():
        print(val)

Or wrap the iterations inside a list-comprehension or a generator and unpack them later:

dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
print(*[val for dic in dataList for val in dic.values()], sep='\n')

the possibilities are endless. It's a matter of choice what you prefer.

Answer from MSeifert on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_dictionaries_loop.asp
Python - Loop Dictionaries
You can loop through a dictionary by using a for loop. When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.
๐ŸŒ
Real Python
realpython.com โ€บ iterate-through-dictionary-python
How to Iterate Through a Dictionary in Python โ€“ Real Python
November 23, 2024 - If you use this approach along with the [key] operator, then you can access the values of your dictionary while you loop through the keys: ... In this example, you use key and likes[key] at the same time to access your target dictionaryโ€™s keys and the values, respectively. This technique enables you to perform different operations on both the keys and the values of likes. Even though iterating through a dictionary directly is pretty straightforward in Python, youโ€™ll often find that dictionaries provide more convenient and explicit tools to achieve the same result.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Loops, arrays, dictionaries -- oh my - Python Help - Discussions on Python.org
October 12, 2022 - Is there a way to carry out this set of instructions using a list or dictionary? Or what would your approach be to make the code more efficient? Thx!
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_loop_dictionary_items.asp
Python Loop Through a Dictionary
When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well. Print all key names in the dictionary, one by one: ... Python Dictionaries Tutorial Dictionary Access Dictionary Items Change Dictionary Item Check if Dictionary Item Exists Dictionary Length Add Dictionary Item Remove Dictionary Items Copy Dictionary Nested Dictionaries
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ iterate-over-a-dictionary-in-python
Iterate over a dictionary in Python - GeeksforGeeks
July 11, 2025 - ... statesAndCapitals = { 'Gujarat': ... print(key) ... Using `zip()` in Python, you can access the keys of a dictionary by iterating over a tuple of the dictionary's keys and values simultaneously....
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ python โ€บ python loop through dictionaries
Python Loop Through Dictionaries
February 21, 2009 - This view object provides a dynamic ... values. We can loop through dictionaries using the dict.items() method by iterating over the key-value pairs returned by this method....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ iterate-through-list-of-dictionaries-in-python
Iterate Through List of Dictionaries in Python
Iterate through the list of dictionaries using for loop. Now we use the items() method to access the key?value pairs in each dictionary. Print the Key, Value pairs. list_of_dict = [ {"course": "DBMS", "price": 1500}, {"course": "Python", "price": ...
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ iterate-through-list-of-dictionaries-in-python
Iterate through list of dictionaries in Python - GeeksforGeeks
November 22, 2021 - To Loop through values in a dictionary you can use built-in methods like values(), items() or even directly iterate over the dictionary to access values with keys.How to Loop Through a Dictionary in PythonThere are multipl
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-iterate-over-dictionary-how-to-loop-through-a-dict
Python Iterate Over Dictionary โ€“ How to Loop Through a Dict
March 10, 2023 - The keys() method is compatible with both Python 2 and 3, so it is a good option if you need to write code that works on both versions of Python. In general, using a basic for loop to iterate through the keys of a dictionary is the fastest method of looping through a dictionary in Python.
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ iterate through list of dictionaries and get key and value.
r/Python on Reddit: Iterate through list of dictionaries and get key and value.
May 26, 2016 -

Hello everyone!

I have been unable to get the values out of a list of dictionaries with python. I've tried many things but nothing that is actually useful.

I have:

my_list = [
    { name: 'alex',
       last_name: 'leda'
     }
    { name: 'john',
       last_name: 'parsons'
     }
]

I want to be able to loop through all dictionaries of the list and extract both the key and its corresponding value. Any idea as to how I would be able to accomplish this?

Many thanks!

๐ŸŒ
Finance Train
financetrain.com โ€บ loop-python-dictionaries-numpy-arrays
How to loop over python dictionaries and Numpy arrays
June 4, 2022 - In the previous lessons, we learned about how to loop over lists. In this lesson, we will learn about how to loop over python dictionaries and Numpy arrays.
๐ŸŒ
Python Central
pythoncentral.io โ€บ how-to-iterate-through-a-dictionary-in-python
How to Iterate Through a Dictionary in Python | Python Central
November 19, 2024 - Pandas provides several methods to iterate through a dictionary's keys, values, and key-value pairs (items): ... The items() method is the most commonly used approach, as it allows you to access both the keys and values in a single loop.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Iterate Over Dictionary Keys, Values, and Items in Python | note.nkmk.me
April 24, 2025 - Built-in Types - dict.items() โ€” Python 3.13.3 documentation ยท for k, v in d.items(): print(k, v) # key1 1 # key2 2 # key3 3 ... You can also receive the key-value pairs as (key, value) tuples in the loop.
๐ŸŒ
Career Karma
careerkarma.com โ€บ blog โ€บ python โ€บ iterate through dictionary python: step-by-step guide
Iterate Through Dictionary Python: Step-By-Step Guide | Career Karma
December 1, 2023 - The for loop prints out both the ... key-value pair in a dictionary into a tuple. Using a for loop and the items() method you can iterate over all of the keys and values in a list....
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how can i loop through a list and basically make a dictionary/hashtable with the list items as keys?
r/learnpython on Reddit: How can I loop through a list and basically make a dictionary/hashtable with the list items as keys?
December 24, 2022 -

How can I loop through a list and basically make a dictionary/hashtable with the list items as keys? I was looking at a leetcode solution for finding out which number in a list is a single number. Here's the code :

from collections import defaultdict
class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        hash_table = defaultdict(int)
        for i in nums:
            hash_table[i] += 1
        
        for i in hash_table:
            if hash_table[i] == 1:
                return i

I was wondering if there was an easier way of doing this without using collections and defaultdict

Top answer
1 of 16
7025

key is just a variable name.

for key in d:

will simply loop over the keys in the dictionary, rather than the keys and values. To loop over both key and value you can use the following:

For Python 3.x:

for key, value in d.items():

For Python 2.x:

for key, value in d.iteritems():

To test for yourself, change the word key to poop.

In Python 3.x, iteritems() was replaced with simply items(), which returns a set-like view backed by the dict, like iteritems() but even better. This is also available in 2.7 as viewitems().

The operation items() will work for both 2 and 3, but in 2 it will return a list of the dictionary's (key, value) pairs, which will not reflect changes to the dict that happen after the items() call. If you want the 2.x behavior in 3.x, you can call list(d.items()).

2 of 16
567

It's not that key is a special word, but that dictionaries implement the iterator protocol. You could do this in your class, e.g. see this question for how to build class iterators.

In the case of dictionaries, it's implemented at the C level. The details are available in PEP 234. In particular, the section titled "Dictionary Iterators":

  • Dictionaries implement a tp_iter slot that returns an efficient iterator that iterates over the keys of the dictionary. [...] This means that we can write

    for k in dict: ...
    

    which is equivalent to, but much faster than

    for k in dict.keys(): ...
    

    as long as the restriction on modifications to the dictionary (either by the loop or by another thread) are not violated.

  • Add methods to dictionaries that return different kinds of iterators explicitly:

    for key in dict.iterkeys(): ...
    
    for value in dict.itervalues(): ...
    
    for key, value in dict.iteritems(): ...
    

    This means that for x in dict is shorthand for for x in dict.iterkeys().

In Python 3, dict.iterkeys(), dict.itervalues() and dict.iteritems() are no longer supported. Use dict.keys(), dict.values() and dict.items() instead.