Python Lists

Lists are one of most widely used data structure in python!

List is a a type of sequence. Essentially, it is a sequence of alphanumeric values separated by comma inside big brackets [ ].

Important thing to note about a list is that items in a list can be of different datatypes. So a single list can contain numbers, strings, boolean values and even a list!

Creating a list in python is as simple as putting different comma-separated values between square brackets.

Sample Output

How to create lists in python
How to create lists in python


Python Lists are mutable

Lists can be changed dynamically. It is often the top choice for storing the computed values inside loops. You can alter a list by changing one of its values on the go.

Sample Output


A List can contain a list!

Python lists can accommodate any other data object as one of the elements in it, you can have even a list as an element, a string, a dictionary, a tuple, a number etc!

Bottomline: A list can contain anything in it!

In the below example the complex list has first two elements as lists and the last element as a string.

Now the lists inside can also contain lists or tuples inside them hence creating an inception of data structures! 🙂

Sample Output

Python lists can contain a list in it
Python lists can contain a list in it


Access elements from the list inside a list

In order to access the elements from complex objects inside a list, we use another layer of indexing.

The index in the first bracket takes out the list present in that index and then the index in the second bracket specifies which exact element.

For example, as shown below, to access the value ‘400’ first we reach to the list which contains it using index “1” as it is present in the second position, then we go “inside” this list by another set of index on top of it!

The same logic applies to any object inside a list, it can be a string, dictionary or tuples as well.

Sample Output

Access list inside a list
Access list inside a list


Commonly used list commands

Once you create any python variable, it gets (“inherits”) many functions ready to use from its parent class. You can see that list using tab button after a dot. As shown below, once you create a list variable, it has many functions associated with it.

Dot + Tab button shows all available functions for the list.
Dot + Tab button shows all available functions for the list.

I am listing here some of the commonly used commands for lists

Sample Output

Author Details
Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Being a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the key business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!

Leave a Reply!

Your email address will not be published. Required fields are marked *