Open In App

How to iterate through a nested List in Python?

Last Updated : 08 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to iterate through a nested List. A list can be used to store multiple Data types such as Integers, Strings, Objects, and also another List within itself. This sub-list which is within the list is what is commonly known as the Nested List.

Iterating through a Nested List

Lets us see how a typical nested list looks like :

list=[10, 20, 30, 40, [ 80,60, 70 ] ]

There are multiple ways to iterate through a Nested List:

Method 1: Use of the index to iterate through the list

Use of Positive Index:

Python3

# code
list = [10, 20, 30, 40, [80, 60, 70]]
  
# Printing sublist at index 4
print(list[4])
  
# Printing 1st element of the sublist
print(list[4][0])
  
# Printing 2nd element of the sublist
print(list[4][1])
  
# Printing 3rd element of the sublist
print(list[4][2])

                    

Output:

[80, 60, 70]
80
60
70

Use of Negative Index

Python3

# code
list = [10, 20, 30, 40, [80, 60, 70]]
  
# Printing sublist at index 4
print(list[-1])
  
# Printing 1st element of the sublist
print(list[-1][-3])
  
# Printing 2nd element of the sublist
print(list[-1][-2])
  
# Printing 3rd element of the sublist
print(list[-1][-1])

                    

Output:

[80, 60, 70]
80
60
70

Method 2: Use of loop to iterate through the list

Python3

# code
# LIST
list = [["Rohan", 60], ["Aviral", 21], 
        ["Harsh", 30], ["Rahul", 40],
        ["Raj", 20]]
  
# looping through nested list using indexes
for names in list:
    print(names[0], "is", names[1],
          "years old.")

                    

Output:

Rohan is 60 years old.
Aviral is 21 years old.
Harsh is 30 years old.
Rahul is 40 years old.
Raj is 20 years old.

Use of Temporary Variables inside a loop.

Python3

# code
# LIST
list = [["Rohan", 60], ["Aviral", 21], 
        ["Harsh", 30], ["Rahul", 40],
        ["Raj", 20]]
  
# looping through nested list using multiple 
# temporary variables
for name, age in list:
    print(name, "is",
          age, "years old.")

                    

Output:

Rohan is 60 years old.
Aviral is 21 years old.
Harsh is 30 years old.
Rahul is 40 years old.
Raj is 20 years old.

Method 3: Use of Slicing

Python3

# code
# list
list = [10, 20, 30, 40,
        [80, 60, 70]]
  
# print the entire Sublist at index 4
print(list[4][:])
  
# printing first two element
print(list[4][0 : 2])

                    

Output:

[80, 60, 70]
[80, 60]


Previous Article
Next Article

Similar Reads

Iterate Through Nested Json Object using Python
Working with nested JSON objects in Python can be a common task, especially when dealing with data from APIs or complex configurations. In this article, we'll explore some generally used methods to iterate through nested JSON objects using Python. Iterate Through Nested Json ObjectBelow, are the method of Iterate Through Nested JSON Object in Pytho
3 min read
Python - Iterate through list without using the increment variable
Python Lists is much like flexible size arrays, declared in other languages like vector in C++, array list in Java, etc. Lists are heterogeneous, making it the most effective feature in Python. Lists are mutable, and hence can be modified even after they have been formed. The most common approach is to iterate through a list using the increment var
2 min read
How we can iterate through list of tuples in Python
In this article, we will discuss different ways to iterate the list of tuples in Python. It can be done in these ways: Using Loop.Using enumerate().Method 1: Using Loop Here we are going to form a list of tuples using for loop. C/C++ Code # create a list of tuples with student # details name = [('sravan',7058,98.45), ('ojaswi',7059,90.67), ('bobby'
2 min read
Iterate through list of dictionaries in Python
In this article, we will learn how to iterate through a list of dictionaries. List of dictionaries in use: [{'Python': 'Machine Learning', 'R': 'Machine learning'}, {'Python': 'Web development', 'Java Script': 'Web Development', 'HTML': 'Web Development'}, {'C++': 'Game Development', 'Python': 'Game Development'}, {'Java': 'App Development', 'Kotli
3 min read
Python | Check if a nested list is a subset of another nested list
Given two lists list1 and list2, check if list2 is a subset of list1 and return True or False accordingly. Examples: Input : list1 = [[2, 3, 1], [4, 5], [6, 8]] list2 = [[4, 5], [6, 8]] Output : True Input : list1 = [['a', 'b'], ['e'], ['c', 'd']] list2 = [['g']] Output : False Let's discuss few approaches to solve the problem. Approach #1 : Naive
7 min read
Python | Iterate through value lists dictionary
While working with dictionary, we can have a case in which we need to iterate through the lists, which are in the keys of dictionaries. This kind of problem can occur in web development domain. Let's discuss certain ways in which this problem can be solved. Method #1: Using list comprehension List comprehension can be used to perform this particula
4 min read
How to iterate through Excel rows in Python?
In this article, we are going to discuss how to iterate through Excel Rows in Python. In order to perform this task, we will be using the Openpyxl module in python. Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The openpyxl module allows a Python program to read and modify Excel files. We wil
2 min read
How to iterate through images in a folder Python?
In this article, we will learn how to iterate through images in a folder in Python. Method 1: Using os.listdirExample 1: Iterating through .png onlyAt first we imported the os module to interact with the operating system.Then we import listdir() function from os to get access to the folders given in quotes.Then with the help of os.listdir() functio
2 min read
Iterate Through Dictionary Keys And Values In Python
In Python, a Dictionary is a data structure where the data will be in the form of key and value pairs. So, to work with dictionaries we need to know how we can iterate through the keys and values. In this article, we will explore different approaches to iterate through keys and values in a Dictionary in Python. Iterate Through Dictionary Keys And V
2 min read
Python - How to Iterate over nested dictionary ?
In this article, we will discuss how to iterate over a nested dictionary in Python. Nested dictionary means dictionary inside a dictionary and we are going to see every possible way of iterating over such a data structure. Nested dictionary in use: {'Student 1': {'Name': 'Bobby', 'Id': 1, 'Age': 20}, 'Student 2': {'Name': 'ojaswi', 'Id': 2, 'Age':
3 min read
Practice Tags :
three90RightbarBannerImg