Dictionaries are the most used data structure in Python and one has to often loop through it.
In this article, we take a look at 5 ways to loop a dictionary.
Example 1: Loop Through Dict Keys
week_numbers = {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday'}
for week_number in week_numbers:
print(week_number)
Output:
1
2
3
4
5
Example 2: Loop Through Dict Values
week_names = {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday'}
for week_name in week_names.values():
print(week_name)
Output:
Monday
Tuesday
Wednesday
Thursday
Friday
Example 3: Loop Through Dict Key-Value Pairs
week_days = {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday'}
for week_number, week_name in week_days.items():
print(f"Week {week_number}: {week_name}")
Output:
Week 1: Monday
Week 2: Tuesday
Week 3: Wednesday
Week 4: Thursday
Week 5: Friday
Example 4: Loop Through Sorted Keys
week_numbers = {5: 'Friday', 3: 'Wednesday', 1: 'Monday', 4: 'Thursday', 2: 'Tuesday'}
for week_number in sorted(week_numbers):
print(week_number)
Output:
1
2
3
4
5
Example 5: Loop Through Keys and Indexes
week_names = {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday'}
for index, week_number in enumerate(week_names, start=1):
print(f"Week {week_number} (Index: {index}): {week_names[week_number]}")
Output:
Week 1 (Index: 1): Monday
Week 2 (Index: 2): Tuesday
Week 3 (Index: 3): Wednesday
Week 4 (Index: 4): Thursday
Week 5 (Index: 5): Friday
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Python,
- How to convert int to ASCII in Python
- How to make use of SQLite Module in Python?
- Split a String into Sub-string and Parse in Python
- Python: Pandas Rename Columns with List Example
- How to run Python file from Mac Terminal
- How to Exit a Loop in Python Code
- Python: How to Plot a Histogram using Matplotlib and data as list
- MD5 Hashing in Python
- Jupyter: Safari Cant Connect to the Server localhost:8888/tree
- Fix: AttributeError: str object has no attribute decode. Did you mean: encode?[Python]
- How to Read a binary File with Python
- How to add two float numbers in Python
- Python: How to install YAML Package
- Python: How to Save Image from URL
- What is Markdown in Jupyter Notebook with Examples
- How to change the Python Default version
- 33: Python Program to send an email vid GMail
- How to comment code in Python
- How to Fix AttributeError in Python
- Fix: error: Jupyter command `jupyter-nbconvert` not found [VSCode]
- How to comment out a block of code in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- Import Other Python Files Examples
- Python: How to add Progress Bar in Console with Examples
- 3 Ways to convert bytes to String in Python
More Posts:
- [fix] openssl No such file or directory error C++ - Ubuntu
- [Solution] macOS could not be installed on your computer OSInstall.mpkg appears to be missing or damaged - MacOS
- macOS: Change Weather App Temperature unit from Fahrenheit to Degree - MacOS
- Adding Sub Headings to Bootstrap Header tags - Html
- Facebook Graph API Unavailable - Facebook
- How to run bash command in background - Bash
- Installing Android Studio Dolphin on Mac with Apple (M1/M2) Chip - Android-Studio
- How to Disable EditText Keyboard Android App - Android