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!
More Posts related to Python,
- Python: Convert Date to DateTime
- How to sort a List using Lambda in Python
- Python matplotlib segmentation fault: 11 macOS Big Sur
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to install Python 3.11 on Mac
- How to flatten a nested list in Python
- Python: Pandas Merge DataFrames on Index Example
- How to Run all Cells at Once Jupyter Notebook
- Python - Convert float to String
- How to add borders to tkinter label text
- How to Exit a Loop in Python Code
- [Python] Fix: ValueError: All arrays must be of the same length
- Sorting an array using Bubble Sort in Python Programming
- How to Unzip a file using Python
- Python: Merge DataFrames Pandas Outer Join Example
- Change label (text) color in tkinter
- Convert Float to String in Python
- Fix: fatal error: No such file or directory compilation terminated
- Python: Access index/counter of a for loop iteration
- Import Other Python Files Examples
- How to install Anaconda on Mac (M1/M2 Mac)
- Python Regular Expression to Find All Matches in List
- How to Read a binary File with Python
- How to disable warnings while Python file execution
- Know current Python Version
More Posts:
- Calculate discount amount python code - Python
- Create a Gradle Java Project in VS Code - Gradle
- [Fix] Modern authentication failed here, but youll still be able to sign in. Your status code is 4c7 - Microsoft Teams - Teams
- Python: Append Values as Prefix or Suffix to a Set Elements - Python
- How to delete SNS Topic using AWS CLI - AWS
- [Fix] Java: Type argument cannot be of primitive type generics - Java
- Install Java 17 (LTS) JDK on ARM based M1/M2 Mac Natively - MacOS
- Difference between Sublime Text vs Visual Studio Code (VS Code) - Sublime-Text