Lists – Control Structure In Python – Free Python Tutorial

List,Though Lists are not a part of Python Control Structure, we can’t complete our study of Python Control Structure without the knowledge of Lists. List is a very important part of Python programming.

What is List in Python?

Lists are a type of object in Python. They are used to store an indexed list of items (mostly like Arrays in other programming languages).

A list is created using square brackets, separating items with commas. The certain item in the list can be accessed by using its index in square brackets. As example :

words = ["Hello", "world", "!"]

This is a List in Python. The name of the List is words and it contains “Hello”, “world”, and “!” at 0th, 1st and 2nd position respectively. To access those items we will simply use their index. As example :

print(words[0])
print(words[1])
print(words[2])

As output we will get :

>>>
Hello
world
!

Let’s take another example of List :

newList = ["Hello", "Python", "I", "am", "Rajesh", "Sinha"]
print(newList[0])
print(newList[1])
print(newList[2])
print(newList[3])
print(newList[4])
print(newList[5])

As output, we will get :

>>>Hello
Python
I
am
Rajesh
Sinha

How can we create a List in Python?

In Python, it is very simple to create a List.

  • Creating an Empty List : An empty list is created with an empty pair of square brackets.
    As example :

    EmptyList = []

    This will create an Empty List with the name EmptyList. If we print the above List,

    print(EmptyList)

    We will get the following output :

    >>>[]

    Now we know how to create an Empty List.

  • Creating List with Elements inside : To create such a List, which will already contain the elements,  we just need to pass the elements separated by commas inside the square brackets. In this approach, we don’t need to insert elements in the List after the creation of List. As example :

    ls=[1,2,3,4,5,6]

    A new List is created which has the elements 1, 2, 3, 4, 5, and 6 with the name ls. Now if we print the List ls,

    # This is one approach of printing Lists
    #_______________________________________
    
    print(ls)
    
    # This is another approach of printing Lists
    #___________________________________________
    
    print(ls[0])
    print(ls[1])
    print(ls[2])
    print(ls[3])
    print(ls[4])
    print(ls[5])

    we will get the following output :

    # Output of the first approach
    #_____________________________
    
    >>>[1, 2, 3, 5, 6]
    
    # Output of the second approach
    #______________________________
    
    >>>1
    2
    3
    4
    5
    6

     


Which type of element can a List contain?

Typically, a list will contain items of a single item type (homogeneous elements), but it is also possible to include several different types (heterogeneous elements). In simple words, a List can contain different types of elements in it.

As example :

Example – 1 : Homogeneous List –>

NumList = [1,2,3,4,5]

StrList = ["Rajesh","Sinha","Padhle"]

boolList = [True,True,False,False]

All the Lists shown above are homogeneous kind of Lists, as all of them contains elements of the same type.

Example 2 : Heterogeneous List –>

HeteroDemo1 = [1,"Rajesh",True,4.5]

HeteroDemo2 = [2,"Sinha",False,5.4]

The above two Lists are heterogeneous Lists, as they contain different types of elements in it.


Nested Lists in Python 

Nested Lists are nothing but a List containing another List in it. They are often called as List of List. Lists of lists are often used to represent 2D grids, as Python lacks the multidimensional arrays that would be used for this in other languages.

Let’s take an example of a Nested List :

NestList = [1,2,[3,4,5],6]

Carefully observe the above List. It contains numeric values in the 0th, 1st and 3rd indexes, but in the 2nd index, it contains another List. So it is a Nested List. Now let’s print the elements from the List.

print(NestList[0])
print(NestList[1])
print(NestList[2])
print(NestList[3])

As output we will get :

>>>1
2
[3,4,5]
6

Note one thing, the inner List is at index 2, though it contains 3 elements in it, for that reason, it will not reserve the indexes 2, 3, and 4 in the main List (NestList). It will be located only at index 2.

To access elements of the inner List, after the name of the main List, there will be two pairs of square brackets, in the first bracket there will be the index of the inner List inside the main List, and in the second bracket there will be the indexes of the elements of the inner List.

As example :

print(NestList[2][0])
print(NestList[2][1])
print(NestList[2][2])

As output we will get :

>>>3
4
5

Note : String objects in Python posses a special character. We can iterate through a String objects like a List, at that time the String object behaves like a Character Array.

As example :

DemoStr = "Rajesh"
print(DemoStr[0])
print(DemoStr[1])
print(DemoStr[2])
print(DemoStr[3])
print(DemoStr[4])
print(DemoStr[5])

As output we will get :

>>>R
a
j
e
s
h

Observe carefully, in this case, the String object DemoStr is behaving like a List of characters. This property only belongs to String objects in Python. If we try to do the same with other types of objects (like integers) we will get an error.

Leave a Comment

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