Beginners Guide to Python 3 – Python Nested List

Python Nested List

A list can contain any sort object, even another list (sublist), which in turn can contain sublists themselves, and so on. This is known as nested list.

You can use them to arrange data into hierarchical structures.

Create a Nested List

A nested list is created by placing a comma-separated sequence of sublists.

L = ['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', 'h']

Access Nested List Items by Index

You can access individual items in a nested list using multiple indexes.

The indexes for the items in a nested list are illustrated as below:

Python Nested List Indexing
L = ['a', 'b', ['cc', 'dd', ['eee', 'fff']], 'g', 'h']

# Prints ['cc', 'dd', ['eee', 'fff']]

# Prints ['eee', 'fff']

# Prints eee

Negative List Indexing In a Nested List

You can access a nested list by negative indexing as well.

Negative indexes count backward from the end of the list. So, L[-1] refers to the last item, L[-2] is the second-last, and so on.

The negative indexes for the items in a nested list are illustrated as below:

Python Nested List Negative Indexing
L = ['a', 'b', ['cc', 'dd', ['eee', 'fff']], 'g', 'h']

# Prints ['cc', 'dd', ['eee', 'fff']]

# Prints ['eee', 'fff']

# Prints eee

Change Nested List Item Value

You can change the value of a specific item in a nested list by referring to its index number.

L = ['a', ['bb', 'cc'], 'd']
L[1][1] = 0
# Prints ['a', ['bb', 0], 'd']

Add items to a Nested list

To add new values to the end of the nested list, use append() method.

L = ['a', ['bb', 'cc'], 'd']
# Prints ['a', ['bb', 'cc', 'xx'], 'd']

When you want to insert an item at a specific position in a nested list, use insert() method.

L = ['a', ['bb', 'cc'], 'd']
# Prints ['a', ['xx', 'bb', 'cc'], 'd']

You can merge one list into another by using extend() method.

L = ['a', ['bb', 'cc'], 'd']
# Prints ['a', ['bb', 'cc', 1, 2, 3], 'd']

Remove items from a Nested List

If you know the index of the item you want, you can use pop() method. It modifies the list and returns the removed item.

L = ['a', ['bb', 'cc', 'dd'], 'e']
x = L[1].pop(1)
# Prints ['a', ['bb', 'dd'], 'e']

# removed item
# Prints cc

If you don’t need the removed value, use the del statement.

L = ['a', ['bb', 'cc', 'dd'], 'e']
del L[1][1]
# Prints ['a', ['bb', 'dd'], 'e']

If you’re not sure where the item is in the list, use remove() method to delete it by value.

L = ['a', ['bb', 'cc', 'dd'], 'e']
# Prints ['a', ['bb', 'dd'], 'e']

Find Nested List Length

You can use the built-in len() function to find how many items a nested sublist has.

L = ['a', ['bb', 'cc'], 'd']

# Prints 3

# Prints 2

Iterate through a Nested List

To iterate over the items of a nested list, use simple for loop.

L = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]   
for list in L:
    for number in list:
        print(number, end=' ')
# Prints 1 2 3 4 5 6 7 8 9


Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.