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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
##### Creating Lists #### # Creating alphanumeric list mylist1 = ['physics', 'chemistry', 1997, 2000] print(mylist1) print(type(mylist1)) # Creating a numeric list mylist2 = [1, 2, 3, 4, 5 ] print(mylist2) # Creating a character list mylist3 = ["a", "b", "c", "d"] print(mylist3) |
Sample Output

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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#### lists are mutable #### # defining a list list1=[1,2,3,4,5] print(type(list1)) # printing one of the values print(list1[1]) # changing that value to other value list1[1]=10 # mutation # printing the list after mutation print(list1) |
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
1 2 3 4 5 6 7 8 9 |
#### Creating a complex list #### # creating sample lists list1=[1,2,3,4,5] list2=[100,200,300,400] # creating a complex list complexList=[list1,list2,'hello'] print(complexList) |

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.
1 2 3 4 5 6 7 8 9 10 11 |
#### Accessing list inside a list #### # creating sample lists list1=[1,2,3,4,5] list2=[100,200,300,400] # creating a complex list complexList=[list1,list2,'hello'] print(complexList) # Accessing values from a list inside a list complexList[1][3] |
Sample Output

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.

I am listing here some of the commonly used commands for lists
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
list1=[1,2,3,4,5] list2=[100,200,300,400] complexList=[list1,list2,'hello'] print(complexList) # Accessing values from a list inside a list print(complexList[1][3]) # counting the number of items in a list print('No. of elements in the list are: ',len(complexList)) # Adding another value to the list complexList.append(500) print('After appending:',complexList) # Adding a list of values to the list complexList.extend([1000,5000]) print('After Extending:',complexList) # Removing a value from the list complexList.remove(1000) print('After Removing:',complexList) # Joining a list of words as single string stringList=['this', 'is', 'a', 'string'] "_".join(stringList) |
Sample Output
