
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 Through a Nested List in Python
In this article, we will understand the concept of nested Lsit in Python and how to iterate through a nested list in Python language, Sometimes, the task is to go through each element of a list or to find the elements in a list or to use the information given in a list. In all these cases, one has to iterate through a list. The list can be made up of a single type of elements or multiple types of elements can be used in a list. The three different examples would be used to represent the different ways to iterate through a list are given.
Nested List Examples ?
A nested list with different item types.
nestedList1 =[["Meena", 30, ["Lucknow", "UP"]], ["Arun", 40, ["Rampur", "UP"]], ["Hina", 35, ["Rohini", "Delhi"]],["Aman", 45, ["Govindpuri", "Delhi"]]]
A 3 dimensional list ?
nestedList2 = [ [ ['a', 'b'], ['c', 'd'] ],[ ['e', 'f'], ['g', 'h'] ] ]
Example 1: Iterate Through a Nested List in Python by Using Element's Index Values
Algorithm
Step 1 ? First specify a nested list to be used. The nested list is a list that has lists as elements.
Step 2 ? Use a list that shows different types of elements, for example: Use the following list in this example ?
nestedList1 =[["Meena", 30, ["Lucknow", "UP"]], ["Arun", 40, ["Rampur", "UP"]], ["Hina", 35, ["Rohini", "Delhi"]],["Aman", 45, ["Govindpuri", "Delhi"]]]
Step 3 ? First print the specified Nested List.
Step 4 ? To Iterate through this list use the index values for example item[0] or item[2][0]
Step 5 ? Use the fetched information in different ways as per the requirement.
Example
nestedList1 =[["Meena", 30, ["Lucknow", "UP"]], ["Arun", 40, ["Rampur", "UP"]], ["Hina", 35, ["Rohini", "Delhi"]],["Aman", 45, ["Govindpuri", "Delhi"]]] print("Printing the nested array: ") print("\n", nestedList1, "\n") print("Printing the information fetched from the nested array: \n") for item in nestedList1: print(item[0], "is", item[1],"years old", "and lives in", item[2][0], ",",item[2][1])
Output
Run the Python File in the Command Window
Open the cmd window. First, we will print the given list. And then individual items are printed.
Example 2: Iterate Through a Nested List in Python by Using Isinstance() to Check Whether the Item is a list
Algorithm
Step 1 ? First specify a nested list to be used. The nested list is a list that has lists as elements.
Step 2 ? Use a list that shows different types of elements, for example: Use the following list in this example ?
nestedList1 =[["Meena", 30, ["Lucknow", "UP"]], ["Arun", 40, ["Rampur", "UP"]], ["Hina", 35, ["Rohini", "Delhi"]],["Aman", 45, ["Govindpuri", "Delhi"]]]
Step 3 ? First print the specified Nested List.
Step 4 ? To Iterate through this list use a for loop to check each item once.
Step 5 ? Use isinstance() function to find if the given item is a list. If it is a list iterate through that inner list.
Example
nestedList1 =[["Meena", 30, ["Lucknow", "UP"]], ["Arun", 40, ["Rampur", "UP"]], ["Hina", 35, ["Rohini", "Delhi"]],["Aman", 45, ["Govindpuri", "Delhi"]]] print("Printing the nested array: ") print("", nestedList1, "\n") for list1 in nestedList1: print("\nPrinting the person's record fetched from the nested array: \n") for l in list1: isitalist=isinstance(l, list) if (isitalist==True): for x in range(0, len(l)): print(l[x]) else: print(l)
Output
Run the Python File in the Command Window ?
Open the cmd window. First, we will print the given list. And then individual items are printed.
Example 3: Iterate Through a Nested List in Python by Using Nested "for" Loops
Algorithm
Step 1 ? First specify a nested list to be used. The nested list is a list that has lists as elements.
Step 2 ? Use a list that is similar to a three-dimensional array, for example ?
Use the following list in this example ?
nestedList2 = [ [ ['a', 'b'], ['c', 'd'] ],[ ['e', 'f'], ['g', 'h'] ] ]
Step 3 ? First print the specified Nested List.
Step 4 ? Now use a for loop to iterate through the first level list. Add a second "for" loop within the first loop to iterate through the second level list. That is the list inside the first list.
Step 5 ? Add the next loop within the first loop to iterate through the second level list. That is the list inside the first list.
Step 6 ? Now use another for loop inside that to iterate the elements of the innermost nested list. Print all the elements in different lines.
Example
nestedList2 = [ [ ['a', 'b'], ['c', 'd'] ], [ ['e', 'f'], ['g', 'h']]] # print the given list print(nestedList2) #the first loop for s in range(len(nestedList2)) : #second loop for t in range(len(nestedList2[s])) : #third loop for u in range(len(nestedList2[s][t])) : print(nestedList2[s][t][u],"\n")
Output
Run the python file in the Command window ?
Open the cmd window. First, we will print the three-dimensional list. And then individual items are printed.
In this Python article, by three different examples, the ways to show how to iterate a nested list, are given. The first method uses the item index values for iteration and shows how to format the output while using the information fetched. In the second example, the isinstance() function is used to find if the given item is a list and then iterate through it further. The last example uses nested "for" loops to iterate items in the three-dimensional nested list.