Python

Python Lists


The list is one of the versatile data structures in Python that arranges the elements in a sequence. The list may include different element types. As the elements are arranged in a sequence, every element has a specific position. The positions of the list are called indices. The index number starts from zero. For example, if we have a list of 10 elements, then the position of elements or index numbers of the list is from 0 to 9. The main feature of the list in Python is that it is not necessary to insert the same elements in a list. The elements could be of a heterogeneous type, i.e., a list can contain an integer, float point number, complex number, and a string. A list can even contain another list at any position.  This article briefly explains the lists in Python, how the list is created updated, the deletion of elements from the list, and so on.

List Creation in Python

A list in Python is created using the pair of square brackets ([]). The elements inside the list are separated by a comma (,).

Let us create multiple lists in Python.

#creating a blank or empty list
myList = []
#printing blank list
print(myList)

#creating a list of numbers
myList = [1,2,3,4,5,6,7,8,9]
#printing the list
print(myList)

#creating a list of words
myList = ["Kamran", "Sattar", "Awaisi"]
#printing the list
print(myList)

#creating a list of multiple items
myList = [1,2,"Kamran",10.0,2+3j]
#printing the list
print(myList)

Output

Access the Elements in a List

In order to access a certain value in the list, we need to indicate the index number. For accessing the list elements, we use the pair of square brackets, mention the index number inside the square brackets, and it returns the element.

#creating a blank or empty list
myList = []
#printing blank list
print(myList)

#creating a list of numbers
myList = [1,2,3,4,5,6,7,8,9]
#printing the list elements
#accessing the first elements
print(my
List[0])
#accessing the second elements
print(myList[1])
#accessing the third elements
print(myList[2])
#accessing the fourth elements
print(myList[3])
#accessing the fifth elements
print(myList[4])
#accessing the sixth elements
print(myList[5])
#accessing the seventh elements
print(myList[6])
#accessing the eighth elements
print(myList[7])
#accessing the ninth elements
print(myList[8])
#creating a list of words
myList = ["Kamran", "Sattar", "Awaisi"]
#printing the list elements
print(myList[0])
print(myList[1])
print(myList[2])

#creating a list of multiple items
myList = [1,2,"Kamran",10.0,2+3j]
#printing the list elements
print(myList[0])
print(myList[1])
print(myList[2])
print(myList[3])
print(myList[4])

Output

We can slice a list in Python by using the slicing operator. The colon (:) is known as the slicing operator. The slicing of the Python list returns the multiple items in a given sequence.

Let us use the slicing operator.

#creating a list
myList = [1,2,3,'a','b',4,5,'c',6,7,'d',8,9]
#printing the list elements by slicing
#printing the elements 3rd to 8th
print(myList[2:8])

#printing the elements 1 to 5
print(myList[0:5])
#printing the elements 1 to 5
print(myList[0:5])
#printing the elements 3 to 8
print(myList[2:8])

Output

Updating the List

We can update the list any time by inserting the elements to the list or by updating the value of the item at a specific index of the list. To add the elements in the list, Python offers a built-in append() function. The append() functions add the element at the end of the list. However, we can add the element at any specific index inside the list. By doing so, the existing element will be replaced, and a new element will be added inside the list.

First, let us see the use of the append() function to add the new elements at the end of the list.

#creating a list
myList = [1,2,3,4,5]
#printing the list before adding new elements
print(myList)
#using the append() function
myList.append(6)
myList.append('a')
myList.append(9)
#printing the list
print(myList)

Output

Now, let us update the existing elements in a list.

#creating a list
student = ['Kamran',25,'kamran@xyz.com','MS SE']
#printing the list before updating the elements
print(student)
#updating the value at position 1
student[1]=24
#updating the value at position 2
student[2]="abc@xyz.com"
#printing the updated list
print(student)

Output

Deleting or Removing the Elements from the List

The elements can be deleted or removed from the list easily. There are two ways to delete or remove the elements from the list:

  1. Using the del keywords
  2. Using remove() function

Using the del keyword

First, let’s discuss the del keyword. The del keyword deletes an item of the list at the specified index. The del keyword requires the index number. Therefore, when you are sure about the index of any value, then you use the del keyword.

#creating a list
student = ['Kamran',25,'kamran@xyz.com','MS SE']
#printing the list before deleting the elements
print(student)
#deleting the value at index 0
del student[0]
#deleting the value at index 1
del student[1]
#printing the updated list
print(student)

Output

Using the remove() function

The remove is the built-in function Python that takes the item of the list as an argument and deletes it from the list. In case you do not know exactly the index value of the item, then the remove method is there for you.

Let’s see the use of remove() function.

#creating a list
student = ['Kamran',25,'kamran@xyz.com','MS SE']
#printing the list before deleting the elements
print(student)
#deleting the items from list
student.remove('Kamran')
student.remove('MS SE')
#printing the updated list
print(student)

Output

Determining the Length of a List

The length of a list can be determined by using the built-in len() function. It returns the size of the list. The len() functions take the list object as an argument.

#creating a list
student = ['Kamran',25,'kamran@xyz.com','MS SE']
#printing the length of the list
print("The length of the list is: ",len(student))

Output

Conclusion

Lists are the most common and widely used data structures in Python. They are used to store the data elements in a sequence. The Python list can store heterogeneous types of elements. This article explains the working of the Python list and various Python built-in functions with the help of simple examples.

About the author

Kamran Sattar Awaisi

Kamran Sattar Awaisi

I am a software engineer and a research scholar. I like to write article and make tutorial on various IT topics including Python, Cloud Computing, Fog Computing and Deep Learning. I love to use Linux based operating systems.