Chapter 9
Chapter 9
Creating a List
*Lists are defined using square brackets[]
*The elements in List are separated by commas
Example:List=[1,2,3,4,5]
Characteristics of Lists
*Ordered
*Items in lust are changeable
*List allows duplicates
print(L[2])
Output:8.9
List Operations
1.Concatenation
2.Repetiton
3.Membership Operators
4.Slicing
5..Traversing
1.Concatenation:
Joining of two or more lists and form a new list is called concatenation of lists.
Symbol + operator is used for concatenation of lists it adds the elements at the end
of the list.
L1=[1,2,3]
L2=[4,5,6]
L=L1+L2
print(L)
Output:[1,2,3,4,5,6]
Concatenation can also be performed in different ways in python using
extend()and append()
i.extend()-It extends a list by adding elements of other list at the end
Example:
L1=[1,2,3]
L2=[4,5,6]
L=L1.extend(L2)
prinr(L)
Output:
[1,2,3,4,5,6]
ii.append()
A List items can be appended at the end of other list.
Example:
L1=[1,2,3]
L2=[4,5,6]
L=L1.append(L2)
prinr(L)
Output:
[1,2,3,[4,5,6]]
2.Repetition
To produce duplicate values or to repeat the values by using * symbol.
L=[‘hi’]
L*3
Output: hihihi
3.Membership Operators
in operator:
The in operator is used to check if a element exists in a sequence or not. It returns
true ,if it finds the specified element in a sequence otherwise false.
Example: L=[1,2,3,’cs’]
5 in L
Output: false
3 in L
Output: true
Cs in L
Output: false since Python is case sensitive
not in operator:
This operator returns true,if the element is not present in list ,else it returns false
Example: L=[1,2,3,’cs’]
5 not in L
Output: true
3 not in L
Output: false
Cs not in L
Output:true
4.Slicing
It is a process to obtain sublist from List
Syntax: sequence[start,stop,step]
By default start value:0
stop value: exclusive
step value: 1
example:
L=[10,20,30,60,80]
L[1:3]
Output:20,30
L[2:]
Output:30,60,80
L[:4]
Output: 10,20,30,60
L[8:2]
Output: error
L[::-1]
Output:80,60,30,10
L[-4:-1]
Output:
20,30,60
L[-1:-4]
Output:
Error
5.Traversing
A Process of visiting and accessing of each element of List is called List
Traversing.
It can be done by using looping statements
1.for loop
2.while loop
Example:
L=[1,2,3,4,5]
for i in L:
print(i)
Output:
1
2
3
4
5
Example:
L=[10,20,30,40]
i=0
while i<len(l):
print(L[i])
i+=1
Output: 10 20 30 40 50
List manipulations
Method Description Example
len() Returns the length of the >>>L=[1,2,3,4]
list passed as the >>>print(L)
argument 4
list() Creates an empty list if >>>L=list()
no argument is passed []
append() Appends given element >>>L1=[1,2,3]
passed as an argument at L1.append([4,5,6])
the end of the list. [1,2,3,[4,5,6]]
extend() It extends the list by >>>L1=[1,2,3]
adding given elements to L1.extend([4,5,6])
the end of the list. [1,2,3,4,5,6]
insert() Inserts an element at L1=[1,2,3]
specified position in the L1.insert(2,6)
list [1,2,6,3]
Nested List
It is a list that contains another list as its elements.
Example:
L=[1,2,3,[‘cs’,4.5],8]
To access the element of the nested list of we need to use two indices.
NestedList[i][j]. The first index i denotes nested list and second index j denotes
element in that nested list.
Example:
L=[1,2,3,4,[7,8,9]]
print(L[4][2])
Output:
9
Copying list
Copying means generating duplicate lists.
Following are the methods to create a copy of the lists.
1.Use of slicing
2.Using List()
3.Using built in copy()
Slicing:Original list can be copied to new list using slicing.
Syntax: Newlist=Oldlist[:]
Example:
L1=[1,2,3]
L2=L1[:]
print(L2)
Output: [1,2,3]
Using List()
A built-in list() also can be used to copy the list
Syntax:Newlist=List(oldlist)
Example:
L1=[1,2,3]
L2=List(L1)
print(L2)
Output: [1,2,3]
Using built in copy()
It is used for copying the list as follows
import copy()
syntax: copy.copy(Oldlist)
Example:
import copy
L1=[1,2,3]
L2=copy.copy(L1)
print(L2)
Output: [1,2,3]