Chapter-3 List
The most basic data structure in Python is the sequence. Each element of a sequence is
assigned a number - its position or index. The first index is zero, the second index is one,
and so forth. Lists are mutable in nature which means we can change their elements.
The list is a most versatile datatype available in Python which can be written as a
list of comma-separated values (items) between square brackets. Important thing about a
list is that items in a list need not be of the same type.
The most basic data structure in Python is the sequence. Each element of a sequence is
assigned a number - its position or index. The first index is zero, the second index is one,
and so forth. Python has six built-in types of sequences, but the most common ones are
lists and tuples,
How to create a list?
In Python programming, a list is created by placing all the items (elements) inside a square bracket [ ],
separated by commas. It can have any number of items and they may be of different types (integer,
float, string etc.).
Empty List
lt=[]
print (lt)
output
[]
Empty List using constructor:
l=list()
print(l)
output
[]
Creating list with same type
lt=[2,4,5]
lt1=['hello','hi','bye']
lt2=[25.7,35.8,93.2]
print (lt)
print (lt1)
print (lt2)
List within list:
lt=[[2,4,5],['hello','hi','bye'],25.7,35.8,93.2]
print (lt) # [[2, 4, 5], ['hello', 'hi', 'bye'], 25.7, 35.8, 93.2]
List with different data types
lt=[1,'rahul',18,87.5]
print (lt) #[1, 'rahul', 18, 87.5]
Access Elements in list: The index operator [] is used to access an item in a list. Index starts from 0.
lt=[2,4,5]
lt1=['sun','mon','tue']
lt2=[['one','two','three'],[4,5,6]]
print(lt[0]) # 2
print(lt1[-2]) #mon
print(lt2[0][1]) #two
print(lt2[-1][-1]) #6
print(lt2[0][1]) # two
Using for loop:
lt=[2,4,5]
lt2=[['one','two','three'],[4,5,6]]
for i in lt:
print(i)
for i in lt2:
for j in i:
print(j,end="")
print()
Using slicing:
lt=[2,4,5,8,10,15]
print(lt[1:2])
print(lt[:3])
print(lt[2:])
print(lt[:])
print(lt[2:‐2])
output:
[4]
[2, 4, 5]
[5, 8, 10, 15]
[2, 4, 5, 8, 10, 15]
[5, 8]
Changed Item Value: We can use assignment operator (=) to change an item or a range of items.
Example1: lt=[2,4,5,8,10,15]
lt2=[['one','two','three'],[4,5,6]]
lt[1]=9
lt2[0][2]=2
lt2[‐1][0]=5
print(lt) # [2, 9, 5, 8, 10, 15]
print(lt2)# [['one', 'two', 2], [5, 5, 6]]
Example 2:
lt=[1,0,5,8,10,15]
lt2=[['one','two','three'],[4,5,6]]
lt[1]=lt[0]
lt[0]=lt[1]
lt2[-1][0]=lt[2]+3
print(lt)# [1, 1, 5, 8, 10, 15]
print(lt2)# [['one', 'two', 'three'], [8, 5, 6]]
Addition of Elements:
By using append() method: It inserts the element at the end of the list.
lt=[1,0,5,8,10,15]
lt2=[['one','two','three'],[4,5,6]]
[Link](25)
[Link](7)
for i in range (1,4):
[Link](i*2)
print(lt) # [1, 0, 5, 8, 10, 15, 25, 2, 4, 6]
print(lt2)# [['one', 'two', 'three'], [4, 5, 6], 7]
Using insert() method: It inserts the element at specific index in the list.
lt=[1,0,5,8,10,15]
lt2=[['one','two','three'],[4,5,6]]
[Link](1,15)
[Link](1,8)
print(lt) # [1, 15, 0, 5, 8, 10, 15]
print(lt2)# [['one', 'two', 'three'], 8, [4, 5, 6]]
Using extend methods : Addition of multiple elements to the List at the end.
lt=[1,0,5,8,10,15]
lt2=[['one','two','three'],[4,5,6]]
[Link]([20,34,66])
[Link]([4,8,6])
[Link](range(15,20))
print(lt)# [1, 0, 5, 8, 10, 15, 20, 34, 66, 15, 16, 17, 18, 19]
print(lt2)# [['one', 'two', 'three'], [4, 5, 6], 4, 8, 6]
Remove Items from List:
Using remove() method: It removes the given the given element from the list.
Example1 lt=[1,0,5,8,10,5]
[Link](8)
print(lt)# [1, 0, 5, 10, 5]
Example2:
lt=[1,0,4,5,8,2,3,10,5]
for i in range(1,5):
[Link](i)
print(lt)# [0, 5, 8, 10, 5]
Using pop() method: The pop() method removes the specified index, (or the last item if index is not
specified):
lt=[1,0,4,5,8,2,3,10,5]
[Link]() #remove last no
[Link](2) #remove no of index no 2
[Link](4)
print(lt)# [1, 0, 5, 8, 3, 10]
using clear() method: Removes all items from the list
lt=[1,0,4,5,8,2,3,10,5]
[Link]()
print(lt) #[]
Using del: The del keyword removes the specified index
lt=[1,0,4,5,8,2,3,10,5]
del lt[1]
print(lt)# 1, 4, 5, 8, 2, 3, 10, 5]
The del keyword can also delete the list completely
lt=[1,0,4,5,8,2,3,10,5]
del(lt)
print(lt)# name 'lt' is not defined
Sum,min,max,count:
lt=[1,0,4,5,8,2,3,10,5]
a=sum(lt)
b=max(lt)
c=min(lt)
d=[Link](5)
print (a)# 38
print (b)# 10
print (c)# 0
print (d)# 2
Practice Previous year question
Q.1 Rewrite the following code in python after removing all syntax error(s). Underline each correction done in
the code.
250 = Number
WHILE Number<=1000:
if Number=>750:
print Number
Number=Number+100
else
print Number*2
Number=Number+50