list manipulation in python class 11 notes
chapter-7
list manipulation in python
class 11 notes
Introduction:-
view web of class 11 list manipulation notes
(list manipulation in python programs)
the python lists are containers that are used to store a list of values of any type. Unlike other variables python lists are mutable i.e., you can can change the elements of a list in place; python will not create a fresh list when you make changes to an element of a list.
List:-
list are mutable sequence of python i.e., you can change elements of a list in place.
Creating and Accessing lists:-
[] # list with no number, empty list
[1,2,3] # list of integer
[1,2,5,3.4,7,9] # list of number(integer and floating point)
[a,b,c] # list of character
[‘one’,’two’,’three’] # list of strings
Creating lists
L=list(<sequence>)
example:
(i)
L=list(input(‘enter list elements : ‘))
enter list elements: 234567
>>>L
[‘2’,’3',’4',5',’6',’7']
(ii)
>>>L=eval(input(“enter list to be added : “))
enter list elements: [2,3,4,5,6,7]
>>>L
[‘2’,’3',’4',5',’6',’7']
Nested lists:-
L1=[3,4,[5,6],7]
Accessing lists:-
* length:
function len(L) returns the number of items (count) in the list L.
* indexing and slicing:
L[i] returns the item at index i(the first item has index 0), and
L[i:j] returns a new list, containing the objects at indexes between i and j (excluding index j).
* membership operators
both ‘in’ and ‘not in’ operator work on lists just like they work for other sequences.
that is, in tells if an element is present in list or not, and not in does the opposite.
* concatenation and replication operator + and * :-
the + operator adds one list to the end of another. The * operator repeats a list. We shall be talking about these two operations in a later section 7.3 — list operations.
Accessing individual elements:-
>>>vowels=[‘a’,’e’,’i’,’o’,’u’]
>>>vowels[0]
‘a’
>>>vowels[-1]
‘u’
Difference from strings:-
although lists are similar to strings in many ways, yet there is an important difference in mutability of the two. Strings are not mutable, while list are. You cannot change individual elements of a string place, but lists allow you to do so. That is, following statement is fully valid for lists(though not for strings):
>>>vowels[0]=’A’
>>>vowels
[‘A’,’e’,’i’,’o’,’u’]
Traversing a list:-
L=[‘p’,’y’,’t’,’h’,’o’,’n’]
for a in L:
print(a)
The above loop will produce result as:
p
y
t
h
o
n
list manipulation in python
Q 1. program to print elements of list[‘q’,’w’,’e’,’r’,’t’,’y’] in separate lines along with element’s both indexes (positive and negative)
ans. L=[‘q’,’w’,’e’,’r’,’t’,’y’]
length=len(L)
for a in range(length):
print(“At indexes”,a,”and”,(a-length),”element :”,L[a])
Sample run above program is :
At indexes 0 and -6 element : q
At indexes 1 and -5 element : w
At indexes 2 and -4 element : e
At indexes 3 and -3 element : r
At indexes 4 and -2 element : t
At indexes 5 and -1 element : y
Comparing lists:-
we can compare two lists using standard comparison operator of python, i.e.,<,>,==,!=, etc.
consider following examples :
>>>L1,l2=[1,2,3],[1,2,3]
>>>L3=[1,[2,3]]
>>>L1==L3
false
>>>L1==L22
true
List operations:-
(1) joining lists
>>>lst1=[1,3,5]
>>>lst2=[6,7,8]
>>>lst1 + lst2
(2)Repeating or Replicating:-
>>>lst1*3
[1,3,5,1,3,5,1,3,5]
(3)Slicing the lists
seq=L[start:stop]
consider the following example:
>>>lst=[10,12,14,20,22,24,30,32,34]
>>>seq=lst[3:-3]
>>>seq
[20,22,24]
seq=L[start:stop:step]
>>>lst
[10,12,14,20,22,24,30,32,34]
>>>lst[0,10:2]
[10,14,22,30,34]
>>>lst[::3]
[10,20,30]
>>>L1=[1,2,3]
L1[10:20]=”abcd”
>>>L1
[1,2,3,’a’,’b’,’c’,’d’]