Chapter 6
Chapter 6
ex:- l = [1,2,[5,6],7]
Output :- [‘h’,’e’,’l’,’l’,’o’]
eval( ) function
=> 25
Ex:- l= [1,2,3]
len(l) => 3
Membership operators
‘in’ – it tells if an element is present in the list or not.
‘not in ’ – just opposite of ‘in’
L=[1,2,3]
2 in L => True
Concatenation and replication operators
+ :- It adds one list to the end of other.
l1=[1,2,3] l2 = [6,7]
l1 + l2 => [1,2,3,6,7]
* :- It repeats a list.
l1 *3 =>[1,2,3,1,2,3,1,2,3]
Traversing a list
To traverse a list ‘for’ loop is used.
Syntax :- for <item> in <list>
process each item here
L=[1,2,3]
for x in L :
print(x)
Output :- 1
2
Comparing lists
L1 = [1,2,3]
L2 = [1,2,3]
L3 = [2,4,6]
L1 ==L2 => True
L1 == L3 => False
Appending element to a list
L.append(item)
ex:- L = [1,2,3]
L.append(4)
print(L) => [1,2,3,4]
Updating element of a list
L[index ]= <new value>
ex:- L = [1,2,3]
L[2] = 10
print(L) => [1,2,10]
Deleting element from a list
L.pop(index) index is optional, if skipped last element is deleted.
ex:- L = [1,2,3]
L.pop( ) => 3
L.pop(1) => 2
Making true copy of a list
ex:- L = [1,2,3]
M=L # now L and M will point to same list
print(L) => [1,2,10]
ex:- L = [1,2,3]
M=L # now L and M will point to same list
To create copy of list :-
L= [1,2,3]
M = list(L)
L[1] = 10
print(L) => [1,10,3]
Joining Lists
Joining two lists is very easy just like we perform addition, literally :)The
concatenation operator +, when used with two lists, joins two lists.
Eg
>>>lst=[1,3,5]
>>>lst=[6,7,8]
>>>lst1+lst2
[1,3,5,6,7,8] // the +operator concatenates two lists and creates a new list
The + operator when used with lists requires that both the operands must be
of list types. We can not add a number or any other value to alist. For example ,
following expressions will result into error:
list+number
list+string
>>> lst=[10,12,14]
>>>lst+2 // Type error : can only concatenate list(not”int”) to list
Like strings, we can only use integer with a * operator when trying to replicate a list
[‘a’,’b’] // list( ) created a list from the keys of the passed dictionary
Eg >>>colours=[“red”,”green”,”blue”]
>>>colours.append(“yellow”)
>>>colours
[“red”,”green”,”blue”,”Yellow”]
The append() does not return the new list, just modifies the original. To
understand this, consider the following example:
>>> lst=[1,2,3]
>>>lst2=1st.append(12)
>>>lst2
>>>lst
[1,2,3,12]
x => 2
remove( ) - it removes the first occurrence of given item from the list.
ex:- L = [1,2,3,4,1]
L.remove(1)
L => [2,3,4,1]
L.remove(5) => error, element not in list
The pop method removes an individual item and returns it, while remove searches for
an item, and removes the first matching item from the list.
List . clear ()
For instance, if we have a list L1 as
>>>L1=[2,3,4,5]
>>>L1.clear( ) //it will remove all the items from list L1.
>>>L1
Unlike del<lstname> statement clear ( ) removes only the element and not the list
element. After clear ( ), the list object still exists as an empty.
count( ) - it returns count of the item in list. If item not found, it return 0.
ex:- L1 = [1,2,3,4,2]
L1.count(2) => 2
Eg L=[13,18,20,10,23]
>>>L1.count(18)
2
>>>L1.count(28)
0
reverse( ) - it reverse the order of items of list.
ex:- L = [1,2,3]
L.reverse( )
L => [3,2,1]
Takes no argument, return no list, reverses the list ‘in place’ and does not
return any thing
>>> t1
[‘e’,’I’,’q’,’a’,’q’,’p’]
>>>t1.reverse( )
>>>t1
[‘p’,’q’,’a’,’q’,’I’,’e’]
>>>t2=[3,4,5]
>>>t3=t2.reverse( )
>>>t3
>>>t2
[5,4,4]
L => [3,2,1]
sorted( ) - This function takes the name of the list as an argument and returns
a new sorted list with sorted element in it.
Eg
>>> val=[17,24,15,30]
>>> sval=sorted (val)
>>>sval
[15,17,24,30]
>>>rsval=sorted (val, reverse=True)
>>>rsval
[30,24,17,15]
count( ) - it returns count of the item in list. If item not found, it return 0.
ex:- L1 = [1,2,3,4,2]
L1.count(2) => 2
Eg L=[13,18,20,10,23]
>>>L1.count(18)
2
>>>L1.count(28)