Python - Loop Dictionaries
Python - Loop Dictionaries
Dark mode
Dark code
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP BOOTSTRAP HOW TO W3.CSS C C++ C# REACT R JQUERY DJANGO
Python Intro
Python Get Started ADVERTISEMENT
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Numbers
Python Casting
Python - Loop Dictionaries
Python Strings ❮ Previous Next ❯
Python Booleans
Python Operators
Python Lists
Python Tuples Loop Through a Dictionary
Python Sets
Python Dictionaries You can loop through a dictionary by using a for loop.
Python Dictionaries When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as
Access Items well.
Change Items
Add Items
Example Get your own Python Server
Remove Items
Loop Dictionaries Print all key names in the dictionary, one by one:
Copy Dictionaries
for x in thisdict:
Nested Dictionaries print(x)
Dictionary Methods
Dictionary Exercise Try it Yourself »
Example
Print all values in the dictionary, one by one:
for x in thisdict:
print(thisdict[x])
Try it Yourself »
COLOR PICKER
Example
You can also use the values() method to return values of a dictionary:
for x in thisdict.values():
print(x)
Try it Yourself »
Example
You can use the keys() method to return the keys of a dictionary:
for x in thisdict.keys():
print(x)
Try it Yourself »
Example
Loop through both keys and values, by using the items() method:
for x, y in thisdict.items():
print(x, y)
Try it Yourself »
ADVERTISEMENT
ADVERTISEMENT
FORUM | ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.