Python Lists

A list is similar to an array that consists of a group of elements or items. Just like an array, a list can store elements. But, there is one major difference between an array and a list. An array can store only one type of elements whereas a list can store different types of elements. Hence lists are more versatile and useful than an array. Perhaps lists are the most used data type in Python programs.

The list appears as given below:

[10, 'Digi Brains Academy', 'M', 50, 55, 62, 74, 66]

Indexing and slicing operations are commonly done on lists. Indexing represents accessing elements by their position numbers in the list. The position numbers start from 0 onwards and are written inside square braces.

CopiedCopy Code

L=[10, 'Digi Brains Academy', 'M', 50, 55, 62, 74, 66]
L[1]	 	#'Digi Brains Academy'
L[0:3:1] 	# [10, 'Digi Brains Academy', 'M']
L[:3:]	 	# [10, 'Digi Brains Academy', 'M']
L[::]	 	# [10, 'Digi Brains Academy', 'M', 50, 55, 62, 74, 66]
L[-2]	 	#74
L[-5:-1] 	# [50, 55, 62, 74]
L[::-1]	 	# [66, 74, 62, 55, 50, 'M', 'Digi Brains Academy', 10]
L[2:]	 	# ['M', 50, 55, 62, 74, 66]

Program: A Python program to create lists with different types of elements.

CopiedCopy Code

num = [10, 20, 30, 40, 50] 
print('Total list = ', num) 
print('First = %d, Last = %d' % (num[0], num[4])) 

names = ["Raju", "Vani", "Gopal", "Laxmi"] 
print('Total list = ', names) 
print('First = %s, Last = %s '% (names[0], names[3])) 

x = [10, 20, 10.5, 2.55, "Ganesh", 'Vishnu'] 
print('Total list = ', x) 
print('First= %d, Last= %s'% (x[0], x[5]))

Create Lists using range() Function:

We can use range() function to generate a sequence of integers which can be stored in a list. The format of the range() function is:

range(start, stop, stepsize)

If we do not mention the 'start', it is assumed to be 0 and the 'stepsize' is taken as 1.

A Python program to create lists using range() function.

CopiedCopy Code

list1 = range(10) 
for i in list1: 
   print(i,',', end='') 
print() #throw cursor to next line 
#create list with integers from 5 to 9
list2 = range(5, 10) 
for i in list2: 
   print(i,',', end='') 
print() 
#create a list with odd numbers from 5 to 9 
list3 = range(5, 10, 2) #step size is 2 
for i in list3: 
   print(i, ',' , end='')

Update elements of List

Lists are mutable. It means we can modify the contents of a list. We can append, update or delete the elements of a list depending upon our requirements. Appending an element means adding an element at the end of the list. To append a new element to the list, we should use the append() method.

CopiedCopy Code

lst = list(range(1,5)) #create a list using list() and range() 
print(lst)
lst.append(9) #append a new element to lst 
print(lst)

Updating an element means changing the value of the element in the list. This can be done by accessing the specific element using indexing or slicing and assigning a new value. Consider the following statements:

CopiedCopy Code

lst[1] = 8 #update 1st element of lst 
print(lst)
lst[1:3] = 10, 11 #update 1st and 2nd elements of lst 
print(lst)

Delete elements of List

Deleting an element from the list can be done using 'del' statement. The del statement takes the position number of the element to be deleted.

CopiedCopy Code

del lst[1] #delete 1st element from lst 
print(lst)

We can also delete an element using the remove() method. In this method, we should pass the element to be deleted. lst.remove(11) #delete 11 from lst print(lst) Let's write a program to retrieve the elements of a list in reverse order. This can be done easily by using the reverse() method, as:

CopiedCopy Code

list.reverse()

This will reverse the order of elements in the list and the reversed elements are available in the list.