You are on page 1of 18

Session 3

Data structure : list


Python lists :
● List is a sequential data structure which has an ordered
collection of values .
● A list is declared by using a pair of square brackets .
● Lists are called mutable as the content of a list can be
modified.
● Syntax :
Listname = [values separated by comma]
Creating python lists :
● We can create empty list as :
○ L1 = []
○ L1 = list()
Here , list() is used for creation of an empty list .
● Examples :
L1 = [1,2,3,4,5]
L1 = [‘a’, ‘b’ , ‘c’ , ‘d’]
L1 = [1 , ‘abc’ , 23.5 , ‘bokaro’ ]
Accessing list contents :
1. By the name of list
The content of a list can be accessed by its name as
L1 = [1,2,3,4,5]
print(L1)
Output :
[1,2,3,4,5]
Accessing list contents :
2. By indexing of list
As there is indexing in string, each item of list are also located
from index 0 onwards.
L1 = [‘a’ , ‘b’ , ‘c’ , ‘d’]
print(L1[0])
print(L1[3])
Output :
a
d
Accessing list contents :
3. By slicing of list
Slicing means accessing a chunk of the list ( a set of consecutive
items of the list) .
Syntax :
Listname [startingvalue : endingvalue ]
L1 = [ ‘b’ , ‘o’ , ‘k’ , ‘a’ , ‘r’ , ‘o’ ] Output :

print(L1[0:2]) ['b', 'o']

print(L1[3:6]) ['a', 'r', 'o']

print(L1[-4:-1]) ['k', 'a', 'r']

print(L1[3: ]) ['a', 'r', 'o']


Modifying list contents :
● We can modify a single or multiple contents of the list by using
the index of list .
● For changing single content :
Listname[ index ] = value
● For changing multiple contents (consecutive items) :
Listname[ starting : ending ] = values separated by comma
L1 = [ ‘b’ , ‘o’ , ‘k’ , ‘a’ , ‘r’ , ‘o’ ] Output :
L1[0] = ‘B’ [ ‘B’ , ‘o’ , ‘k’ , ‘a’ , ‘r’ , ‘o’ ]
print(L1) [ ‘B’ , ‘o’ , ‘k’ , ‘a’ , ‘r’ , ‘OO’ ]
L1[-1] = ‘OO’ [ ‘B’ , ‘o’ , ‘K’ , ‘A’ , ‘R’ , ‘OO’ ]
print(L1)
L1[2:5] = ‘K’ , ‘A’ , ‘R’
print(L1)
Adding item in list using append():
● We use append() to add an item in the list .
● Syntax :
listname.append(value)
● Example :
L1 = [‘q’ , ‘w’ , ‘e’ , ‘r’ , ‘t’ ]
L1.append(‘y’)
print(L1) Output :

[‘q’ , ‘w’ , ‘e’ , ‘r’ , ‘t’ , ‘y’ ]


Adding item in list using extend() :
● We use extend() to merge a list with other list .
● Syntax :
listname.extend([listvalues])
● Example :
L1 = [‘q’ , ‘w’ ]
L1.extend([‘e’, ‘r’])
Output :
print(L1)
L1.append([‘t’ , ‘y’]) [‘q’ , ‘w’ , ‘e’ , ‘r’ ]
print(L1) [‘q’ , ‘w’ , ‘e’ , ‘r’ , [‘t’ , ‘y’] ]
Adding item in list using insert() :
● We use insert() to insert the given item at the given index and
items after it shift forward .
● Syntax :
listname.insert(index , value)
● Example :
L1 = [‘q’ , ‘w’ , ‘r’ , ‘t’ , ‘y’ ]
L1.insert(2 , ‘e’) Output :
print(L1)
[‘q’ , ‘w’ , ‘e’ , ‘r’ , ‘t’ , ‘y’ ]
Removing item from list :
● We use remove() to remove an item from the list . If there are
two items of same value, it removes the first one.
● Syntax :
listname.remove(value)
● Example :
L1 = [‘q’ , ‘w’ , ‘r’ , ‘t’ , ‘y’ ]
L1.remove(‘w’) Output :
print(L1)
[‘q’ , ‘r’ , ‘t’ , ‘y’ ]
Sorting items of list :
● List only with similar type of values can be sorted.
● We use sort() to arrange the list items in ascending order .
● To sort the list in descending order, we use (reverse=True) .
● Syntax :
listname.sort()
listname.sort(reverse=True) Output :
● Example :
L1 = [7,3,9,10,2,6] [2, 3, 6, 7, 9, 10]
L1.sort()
print(L1) [10, 9, 7, 6, 3, 2]
L1.sort(reverse=True)
print(L1)
Reversing items of list :
● We use reverse() to reverse the list .
● Syntax :
listname.reverse()
● Example : Output :
L1 = [7,3,9,10,2,6]
[6, 2, 10, 9, 3, 7]
L1.reverse()
print(L1)
Counting items of list :
● We use count() to find out how many times a value
appears in the list .
● Syntax :
Variable = listname.count(value)
● Example :
L1 = [7,3,9,10,2,6,2,4,2,8,2] Output :
c = L1.count(2) occurrence of 2 = 4
print(“occurrence of 2 =”,c)
Copying a list :
● We use copy() to copy one list to another list .
● We can copy whole list or a slice of a list .
● Syntax :

list1 = list2.copy()
list1 = list2[starting:ending].copy()
list1 = list2[starting:ending]
L1 = [ ‘I’ , ‘N’ , ‘D’ , ‘I’ , ‘A’ , ‘N’] Output :
L2 = L1.copy() [ ‘I’ , ‘N’ , ‘D’ , ‘I’ , ‘A’ , ‘N’]
print(L2) [ ‘I’ , ‘N’ , ‘D’ , ‘I’ , ‘A’ ]
L3 = L1[0:5].copy() [ ‘I’ , ‘A’ , ‘N’]
print(L3)
L4 = L1[-3:-1]
print(L4)

You might also like