A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.w3schools.com/python/gloss_python_loop_dictionary_items.asp below:

Website Navigation


Python Loop Through a Dictionary

Python Loop Through a Dictionary

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.

Example

Print all key names in the dictionary, one by one:

for x in thisdict:
  print(x)

Try it Yourself » Example

Print all values in the dictionary, one by one:

for x in thisdict:
  print(thisdict[x])

Try it Yourself » Example

You can also use the values() function to return values of a dictionary:

for x in thisdict.values():
  print(x)

Try it Yourself » Example

Loop through both keys and values, by using the items() function:

for x, y in thisdict.items():
  print(x, y)

Try it Yourself »

Track your progress - it's free!


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4