list manipulation in python class 11 notes

Contents show

 chapter-7
list manipulation in python

class 11 notes

list manipulation in python class 11 notes

(list manipulation in python)

Introduction:-

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:-

1. []                                        list with no number, empty list

2. [1,2,3]                              list of integer

3. [1,2,5,3.4,7,9]                list of number(integer and floating point)

4. [a, b, c]                             list of character

5. [‘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]

list manipulation in python class 11 notes

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

READ MORE :

Lists in Python Tutorial

list manipulation in python class 11 programs practice more questions

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 :

1. At indexes 0 and -6 element : q

2. At indexes 1 and -5 element : w

3. At indexes 2 and -4 element : e

4. At indexes 3 and -3 element : r

5. At indexes 4 and -2 element : t

6. At indexes 5 and -1 element : y

Q2. Program to find minimum element from a list of element along with its index in list.

program :-

lst = eval(input("Enter list :"))
length = len(lst)
min_ele = lst[0]
min_index = 0
for i in range(1, length-1) :
      if lst[i] < min_ele :
           min_ele = lst[i]
           min_index = i
print("Given list is :", lst)
print("The minimum element of the given list is:")
print(min_ele, "at index", min_index)

Sample run of above program is :

Enter list : [2, 3, 4, -2, 6, -7, 8, 11, -9, 11] Given list is : [2, 3, 4, -2, 6, -7, 8, 11, -9, 11] The minimum element of given list is : -9 at index 8

Comparing lists:-

we can compare two lists using standard comparison operator of python, i.e.,<,>,==,!=, etc.

consider following examples :

1. >>>L1,l2=[1,2,3],[1,2,3]

2. >>>L3=[1,[2,3]]

3. >>>L1==L3

false

4. >>>L1==L22

true

list manipulation in python class 11 notes

List operations:-

(1) joining lists

1. >>>lst1=[1,3,5]

2. >>>lst2=[6,7,8]

3. >>>lst1 + lst2

(2)Repeating or Replicating:-

1. >>>lst1*3

[1,3,5,1,3,5,1,3,5]

(3)Slicing the lists

seq=L[start:stop]

consider the following example:

1. >>>lst=[10,12,14,20,22,24,30,32,34]

2. >>>seq=lst[3:-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’]

Working with lists:-

L.aapend(item)

consider some example:

1. >>>lst1=[10,12,14]

2. >>>lst1.append(16)

3. >>>lst1

[10,12,14,16]

updating element to a list

L[index]=<new value>

consider following example:

1. >>>lst1=[10,12,14,16]

2. >>>lst1[2]=24

3. >>>lst1

[10,12,24,16]

Deleting elements from a list:-

del list[<index>]

del list[<start>:<stop>]

consider following example:

lst=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

>>>del lst[10]

>>>lst

=[1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20]

you can also use pop() method to remove single element.

List.pop(<index>)

return the removed item

consider the example below:

lst=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

>>>lst.pop()

20

>>>lst.pop(10)

11

the pop() method is useful only when you want to store the element being deleting for later use, e.g.,

item1=L.pop()                 # last item

item2=L.pop(0)               #first item

item3=L.pop(5)               #sixth item

List functions and methods:-

<Listobject>.<method name>()

(1)the index method:-

return index of first matched item

List.index(<item>)

for example:

>>>L1=[13,18,11,16,18,14]

>>>L1.index(18)

1

(2) the append method:-

this method adds an item to the end of the list.

List.append(<item>)

for example:

>>>colors=[‘red’,’green’,’blue’]

>>>colors.append(‘yellow’)

>>>colors

[‘red’,’green’,’blue’,’yellow’]

(3) the extend method:-

the extend() method is also used for adding multiple elements to a list.

List.extend(<list>)

consider following example:

>>>t1=[‘a’,’b’,’c’,’d’,’e’]

>>>t2=[‘d’,’e’]

>>>t1.extend(t2)

>>>t1

[‘a’,’b’,’c’,’d’,’e’]

>>>t2

[‘d’,’e’]

Difference between append() and extend()

while append() function adds one element to list, extend can add multiple elements from a list supplied to it as argument.

the extend() cannot add a single element; it require a list as argument.

(4) the insert method:-

the insert() method is also an insertion method for lists, like append and extend methods. However, both append() and extend() insert element(s) at the end of the list. if you wants to insert an element somewhere in between or any position of your choice, both append() and extend() are of no use. For such requirement insert() is used.

List.insert(<pos>,<item>)

the first argument <pos> is the index of the element before which the second argument <item> is to be added.

consider the following example:

>>>t1=[‘a’,’e’,’u’]

>>>t1.insert(2,’i’)

>>>t1

[‘a’,’e’,’i’,’u’]

(5) the pop method:-

List.pop(<index>)

>>>t1=[‘k’,’a’,’e’,’i’,’p’, ‘q’, ‘u’]

>>>ele1=t1.pop(0)

>>>ele1

‘k’

(6) the remove method:-

the remove() method removes the first occurrence of given item in the list.

consider following example:

>>>t1=[‘a’,’e’,’i’,’p’, ‘q’, ‘a’, ‘q’, ‘p’]

>>>t1.remove(‘a’)

>>>t1

[‘e’,’i’,’p’,’q’,’a’,’q’,’p’]

>>>t1.remove(‘p’)

>>>t1

[‘e’,’i’,’q’,’a’,’q’,’p’]

(7) the clear method:-

list.clear()

example:

>>>L1=[2,3,4,5]

>>>L1.clear()

>>>L1

[ ]

(8) the count method:-

List.count(<item>)

example:

>>>L1=[13,18,20,10,18,23]

>>>L1.count(18)

2

>>>L1.count(28)

0

(9) the reverse method:-

List.reverse()

>>>t1=[‘a’,’e’,’i’,’o’,’u’]

>>>t1.reverse()

>>>t1

[‘u’,’o’,’i’,’e’,’a’]

(10) the sort method:-

List.sort()

example:

>>>t1=[‘e’,’i’,’q’,’a’,’q’,’p’]

>>>t1.sort()

>>>t1

[‘a’,’e’,’i’,’p’,’q’,’q’]

for decreasing order using sort

List.sort(reverse=true)

Important Questions

Q 1. How are lists different from strings when both are sequences ?
ans. The lists and strings are different in following ways :

1.  The lists are mutable sequences while strings are immutable.
2.  In consecutive locations, strings store the individual character while list stores the references of its elements.
3.  Strings store single type of elements – all character while list can store elements belonging of its elements.
4. Strings store single type of elements – all character while lists can store elements belonging to different types.

Q 2.What are nested lists ?

ans. When a list is contained in another list as a member-element, it is called nested list, e.g.,
a = [2, 3, [4, 5]]
The above list a has three elements – an integer 2, an integer 3 and a list[4, 5], hence it is nested list.

Q 3. What does each of the following expression evaluate to ?
ans. Suppose that L is the list
[“These”, “are”, “a”, [“few”, “words”], “that”, “we”, “will”, “use”]
(a) 1.  L[3:4]
2. L[3:4][0]
3. L[3:4][0][1]
4. L[3:4][0][1][2]

(b) “few” in L
(c) “few” in L[3]
(d) [L[1]] + L[3]
(e) L[4:]
(f) L[0::2]

ans.
(a) [[“few”, “words”]]
[“few”, “words”]
‘words’
‘r’
(b) False
(c) True
(d) [‘are’, ‘few’, ‘words’]
(e) [“that”, “we”, “will”, “use”]
(f) [“these”, “a”, “that”, “will”]

Q 4. Write the most appropriate list method to perform the following task.
(a) Delete a given element from the list.
(b) Delete 3rd element from the list.
(c) Add an element in the end of the list.
(d) Add an element in the beginning of the list.
(e) Add elements of a list in the end of a list.

ans.
(a) remove()
(b) pop()
(c) append()
(d) insert()
(e) extend()

Q 5. L is a non-empty list of ints. Print the smallest and largest integers in L. Write the code without using a loop.

ans.
Length = len(L)
L.sort()
Print(“smallest :”, L[0])
print(“largest :”, L[length-1])

list manipulation notes downloadpdf

How to Learn Python for beginners ?