

- 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
How to iterate through a dictionary in Python?
There are two ways of iterating through a Python dictionary object. One is to fetch associated value for each key in keys() list.
>>> D1 = {1:'a', 2:'b', 3:'c'} >>> for k in D1.keys(): print (k, D1[k]) 1 a 2 b 3 c
There is also items() method of dictionary object which returns list of tuples, each tuple having key and value. Each tuple is then unpacked to two variables to print one dictionary item at a time.
>>> D1={1:'a', 2:'b', 3:'c'} >>> for k, v in D1.items(): print (k, v) 1 a 2 b 3 c
- Related Questions & Answers
- How to iterate through a list in Python?
- How to iterate through a tuple in Python?
- How to recursively iterate a nested Python dictionary?
- Iterate over a dictionary in Python
- How to iterate over a C# dictionary?
- How we can iterate through a Python list of tuples?
- How to correctly iterate through getElementsByClassName() in JavaScript?
- Iterate through ArrayList in Java
- Loop through a Dictionary in Javascript
- How can I iterate through two lists in parallel in Python?
- Iterate through Java Unit Tuple
- Iterate through Java Pair Tuple
- Iterate through Quartet class in JavaTuples
- Iterate through Triplet class in JavaTuples
- Iterate through Septet class in JavaTuples
Advertisements