×

Traverse Dictionary in Python

Traversing a dictionary is visiting each and every step in the dictionary, which is also called iterating the dictionary

In Python, dictionaries are a useful and commonly used data structure in which we have key-value pairs

Traversing a dictionary explains how to operate on a dictionary's key-value pairs while traversing through it.

There are many ways to traverse the dictionary in python

1. Using the items() method

2. Using the keys() method

3. Using the values() method

4. Using the Index to Iterate

1. Using the items() method

We can loop through the dictionary using Python's dict.items() method. You will learn the value and the key to each thing with each iteration.

Let ‘s see the examples using the items() method

Example 1

dictionary = { 
   'sravani': 'mamidgi', 
   'yamini': 'nallakunta', 
   'glory': 'parshapalli', 
   'chandana': 'ranabothu', 
   'shivapriya': 'thalla'
   }
for keys, values in dictionary.items(): 
print(keys ,values)

output

sravanimamidgi
yamininallakunta
glory parshapalli
chandanaranabothu
shiva priyathalla

Example 2

dictionary = { 
   'Name': 'sravani', 
   'year': '2003', 
   'age': '19', 
   'locatiion': 'Zaheerabad' 
}


for keys, values in dictionary.items(): 
print(keys ,values)

output

Name sravani
year 2003
age 19
locatiionZaheerabad

2. Using the keys() method

Use the keys() method that the dictionary provides to iterate through all of its keys. A list of the keys found in the dictionary is returned as an iterable.

Let ‘s see the examples using the items() method

Example 1

dictionary = { 
   'Name': 'sravani', 
   'year': '2003', 
   'age': '19', 
   'locatiion': 'Zaheerabad' 
}


for keys in dictionary.keys(): 
   print(keys)

output

Name
year
age
location

Example 2

dictionary = { 
   'sravani': 'mamidgi', 
   'yamini': 'nallakunta', 
   'glory': 'parshapalli', 
   'chandana': 'ranabothu', 
   'shivapriya': 'thalla'
   }
for keys in dictionary.keys(): 
   print(keys)

output

sravani
yamini
glory
chandana
shiva Priya

3. Using the values() method

Use the dictionary's values() method to loop through the values of the dictionary's elements.

The dictionary returns an iterable that contains all of the values for each entry. Then, by utilizing a for loop, you can cycle over the numbers as seen below.

Let ‘s see the examples using the items() method

Example 1

dictionary = { 
   'sravani': 'mamidgi', 
   'yamini': 'nallakunta', 
   'glory': 'parshapalli', 
   'chandana': 'ranabothu', 
   'shivapriya': 'thalla'
   }
for values in dictionary.values(): 
   print(values)

output

mamidgi
nallakunta
parshapalli
ranabothu
thalla

example 2

dictionary = { 
   'Name': 'sravani', 
   'year': '2003', 
   'age': '19', 
   'locatiion': 'Zaheerabad' 
}
for values in dictionary.values(): 
   print(values)

output

sravani
sravani
2003
19
Zaheerabad

4. Using the Index to Iterate

To iterate across the dictionary, use the item’s index. This is comparable to iterating the dictionary without using the methods keys(), values(), or items.

Let ‘s see the examples using the items() method

Example 1

dictionary = { 
   'Name': 'sravani', 
   'year': '2003', 
   'age': '19', 
   'locatiion': 'Zaheerabad' 
}
for index in dictionary:
print(index, dictionary[index])

output

Name sravani
year 2003
age 19
locatiionZaheerabad

Example 2

dictionary = { 
   'sravani': 'mamidgi', 
   'yamini': 'nallakunta', 
   'glory': 'parshapalli', 
   'chandana': 'ranabothu', 
   'shivapriya': 'thalla'
   }
for index in dictionary:
print(index, dictionary[index])

output

sravanimamidgi
yamininallakunta
glory parshapalli
chandanaranabothu
shiva priyathalla

Traversing over a dictionary in alphabetical order

Usually, dictionaries don't maintain any kind of structure. This suggests that the order of the elements in the iteration is not guaranteed. To iterate a dictionary using a specified order, use Python's sorted() method. The item will initially be sorted before being traversed through by a for a loop.

Example 1

Dictionary keys are sorted by the function sorted(dictionary.keys()), and for iterates through the keys that were returned.

Here is an example of iterating through an index-based dictionary

dictionary = { 
   'sravani': 'mamidgi', 
   'yamini': 'nallakunta', 
   'glory': 'parshapalli', 
   'chandana': 'ranabothu', 
   'shivapriya': 'thalla'
   }
for keys in sorted(dictionary.keys()):
print(keys, dictionary[keys])

output

chandanaranabothu
glory parshapalli
shiva priyathalla
sravanimamidgi
yamininallakunta

Example 2

dictionary = { 
   'Name': 'sravani', 
   'year': '2003', 
   'age': '19', 
   'locatiion': 'Zaheerabad' 
}


for keys in sorted(dictionary.keys()):
print(keys, dictionary[keys])

output

Name sravani
age 19
locatiionZaheerabad
year 2003

Combine the value and the key unlike using items()

In this illustration, a Python Loop Through a Dictionary is used, and with each iteration, the directory's key is obtained. Next, key data is printed, and a key is used as an index to display values from the directory.

Example 1

dictionary = { 
   'Name': 'sravani', 
   'year': '2003', 
   'age': '19', 
   'locatiion': 'Zaheerabad' 
}


for i in dictionary: 
   print(i,'->',dictionary[i])

output

Name ->sravani
year -> 2003
age -> 19
locatiion ->Zaheerabad

example 2

dictionary = { 
   'sravani': 'mamidgi', 
   'yamini': 'nallakunta', 
   'glory': 'parshapalli', 
   'chandana': 'ranabothu', 
   'shivapriya': 'thalla'
   }
for i in dictionary: 
   print(i,'->',dictionary[i])

output

sravani ->mamidgi
yamini ->nallakunta
glory ->parshapalli
chandana ->ranabothu
shiva priya ->thalla

Related Topics

Weight Conversion GUI using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.

Python Comments

In this tutorial, we will discuss the importance of comments in the Python code and how various types of comments can be inserted in the code. Comments are used to describe...

3 minutes read.

How to set font for Text in Python

As a tuple with the font family as the first component, a size in point as the second, and possibly a string with one or more of the style modifiers...

5 minutes read.

Alexa Python

The cloud-based voice assistant renders text into speech in a female voice. Alexa is a virtual assistant released by Amazon. Using itself as a system for home automation, Alexa can...

4 minutes read.

Python Project Ideas for Advanced Developers

Best Python Project Ideas for Advanced Developers Python is an object-oriented, high-level, interpreted programming language created by a developer known Guido Van Rossum. Python is one of the languages that gathered...

9 minutes read.

Return Statement In Python

Python Return Statement The return statement is generally used for the execution ending and returns the value back to the caller. The return statement can return all types of values and...

2 minutes read.

Python Not Equal Operator

Python provides us with many operators to make tasks easier. There are about 7 categories of operators in Python. One of the 7 classifications is the comparison operators. Just as...

3 minutes read.

Python complex() class

Python complex() class The complex() class in Python returns a complex number or converts a string or number to a complex number. Syntax class complex([real[, imag]]) Parameter Real: This parameter consists of a number representing the real...

1 minute read.

Iterators in Python

Introduction In Python, an iterator is defined as an object that enables traversing through all the values of a collection. It contains the countable number of values. The iterator is utilized to...

4 minutes read.

Python Generator

Python Generator: A Function is said to be a Python Generator that produces or generates a sequence of results. A Python Generator maintains its native state to work so that...

6 minutes read.

How to create a DataFrames in Python

How to create a DataFrames in Python Data Frame is a data structure in which data is stored in tabular form. They can also be referred as two-dimensional collection of data. Various...

5 minutes read.

Android apps for coding in python

Nowadays, it is the generation of smartphones. The world can be seen and acknowledged with a single click on your mobile phone. Everyone uses mobile phones to do any work,...

9 minutes read.

Python Read Excel file

Python Read Excel file Excel is the spreadsheet application for Window, which is developed by Microsoft. The Excel stores data in the tabular form. It provides easy access to analyze and maintain the...

3 minutes read.

Python Modules List

Python is like an ocean. If you have been working with Python, you might’ve already heard the word module. When we solve a problem in mathematics, there will be several...

3 minutes read.

Python Printf Style Formating

String objects have one special implicit activity: the % administrator (modulo). This is otherwise called the string designing or interjection administrator. Given design % values (where configuration is a string),...

3 minutes read.

How to Convert A List into String In Python?

How to Convert A List into String In Python? List is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets...

3 minutes read.

Python Built-in Functions

Python Built-in Functions The Python interpreter has a number of functions and types built into it that are always available. The Python built- in functions are as follow: Methods Explaining abs() The abs() function returns...

6 minutes read.

Writing to a CSV file in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

6 minutes read.

Face Recognition in Python

In this tutorial, we will understand what is face recognition and how it is achieved in python. Face recognition is of great utility in real-world scenarios. It is an extended step...

3 minutes read.

Python getattr() function

Python getattr() function The getattr() function in Python returns the value of the named attribute for the given object. Syntax getattr(object, name[, default]) Parameter object: It is a required parameter which represents an object. name: This parameter represents the...

1 minute read.