
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Iterate over a dictionary in Python
In this article, we will learn about iteration/traversal of a dictionary in Python 3.x. Or earlier.
A dictionary is an unordered sequence of key-value pairs. Indices can be of any immutable type and are called keys. This is also specified within curly braces.
Method 1 − Using iterables directly
Example
dict_inp = {'t':'u','t':'o','r':'i','a':'l','s':'p','o':'i','n':'t'} # Iterate over the string for value in dict_inp: print(value, end='')
Output
trason
Method 2 − Using iterables for values of the dictionary
Example
dict_inp = {'t':'u','t':'o','r':'i','a':'l','s':'p','o':'i','n':'t'} # Iterate over the string for value in dict_inp.values(): print(value, end='')
Output
oilpit
Method 3 − Using keys as an index
Example
dict_inp = {'t':'u','t':'o','r':'i','a':'l','s':'p','o':'i','n':'t'} # Iterate over the string for value in dict_inp: print(dict_inp[value], end='')
Output
oilpit
Method 4 − Using keys and values of the dictionary
Example
dict_inp = {'t':'u','t':'o','r':'i','a':'l','s':'p','o':'i','n':'t'} # Iterate over the string for value,char in dict_inp.items(): print(value,":",char, end=" ")
Output
t : o r : i a : l s : p o : i n : t
Conclusion
In this article, we learned about iteration/traversal over a dictionary in Python.
- Related Questions & Answers
- How to iterate over a C# dictionary?
- Iterate over a list in Python
- Iterate over a set in Python
- What is the best way to iterate over a Dictionary in C#?
- Iterate over characters of a string in Python
- How to iterate through a dictionary in Python?
- How to recursively iterate a nested Python dictionary?
- Python program to iterate over multiple lists simultaneously?
- How to iterate over a Java list?
- How to iterate over a C# list?
- How to iterate over a C# tuple?
- Java Program to Iterate over a HashMap
- Java Program to Iterate over a Set
- Iterate over lines from multiple input streams in Python
- How to iterate over a Hashmap in Kotlin?
Advertisements