0% found this document useful (0 votes)
31 views13 pages

LIST MANIPULATION Practical Live Class

Uploaded by

mrsanketsesai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views13 pages

LIST MANIPULATION Practical Live Class

Uploaded by

mrsanketsesai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Creating list from existing sequences ¶

In [1]: a=list('informatics')
print(a)

['i', 'n', 'f', 'o', 'r', 'm', 'a', 't', 'i', 'c', 's']

In [3]: a=list((10,20,30,40,50))
print(a)

[10, 20, 30, 40, 50]

using eval function

In [7]: list1=eval(input("Enter your list:"))


print(list1)

Enter your list:[10,20,30,40]


[10, 20, 30, 40]

Accessing list elements


In [8]: fruits=["mango","apple","guava","pomegranate","cherry"]
print(fruits)

['mango', 'apple', 'guava', 'pomegranate', 'cherry']

In [9]: print(fruits[-2])

pomegranate

In [10]: print(fruits[4])

cherry
In [11]: print(fruits[6])

-------------------------------------------------------------------------
--
IndexError Traceback (most recent call las
t)
<ipython-input-11-a5ee2976c09a> in <module>()
----> 1 print(fruits[6])

IndexError: list index out of range

Traversing a list
In [14]: L=[1,2,55,34,67,23,11]
for a in L:
print(a,end=" ")

1 2 55 34 67 23 11

Comparing List
In [15]: 10>20

Out[15]: False

In [16]: 20==20

Out[16]: True

In [17]: [10,20]>[10,20,30]

Out[17]: False

In [18]: [10,20,30,40]>[10,20,30]

Out[18]: True

In [19]: [10,20]>[50,20]

Out[19]: False

In [20]: [1,3,5]<[1,5,3]

Out[20]: True
In [22]: [10,20,60,80]>[10,20,30,100]

Out[22]: True

Joining of lists
In [1]: a=[1,2,3]
b=[10,20,30]
c=a+b
print(c)

[1, 2, 3, 10, 20, 30]

In [2]: [1,2,3]+['a','b']

Out[2]: [1, 2, 3, 'a', 'b']

In [3]: ['a','b']+20

-------------------------------------------------------------------------
--
TypeError Traceback (most recent call las
t)
<ipython-input-3-b22b00fcdb37> in <module>()
----> 1 ['a','b']+20

TypeError: can only concatenate list (not "int") to list

Repeating a list
In [4]: list1=[11,'a','b']
print(list1*2)

[11, 'a', 'b', 11, 'a', 'b']

In [6]: list1=[11,'a','b']
print(list1*3.5)

-------------------------------------------------------------------------
--
TypeError Traceback (most recent call las
t)
<ipython-input-6-d0eec9b9faff> in <module>()
1 list1=[11,'a','b']
----> 2 print(list1*3.5)

TypeError: can't multiply sequence by non-int of type 'float'


List slicing
syntax: List[start:stop:step]

In [9]: marks=[34,12,33,26,21,9,11]
print(marks[1:4:1])
print(marks[1:4]) #step is 1 by default

[12, 33, 26]


[12, 33, 26]

In [10]: print(marks[0:6])

[34, 12, 33, 26, 21, 9]

In [11]: marks[0:10]

Out[11]: [34, 12, 33, 26, 21, 9, 11]

In [12]: marks

Out[12]: [34, 12, 33, 26, 21, 9, 11]

In [13]: marks[0:10:2]

Out[13]: [34, 33, 21, 11]

In [14]: marks[:4] # by default it starts with 0 index

Out[14]: [34, 12, 33, 26]

In [15]: marks[2:] # by default it will take upto last value.

Out[15]: [33, 26, 21, 9, 11]

In [16]: marks[5:1]

Out[16]: []

In [17]: marks[5:1:-1]

Out[17]: [9, 21, 26, 33]

In [18]: marks

Out[18]: [34, 12, 33, 26, 21, 9, 11]


In [1]: marks=[34,12,33,26,21,9,11]

In [2]: marks

Out[2]: [34, 12, 33, 26, 21, 9, 11]

In [4]: marks[::]

Out[4]: [34, 12, 33, 26, 21, 9, 11]

In [5]: marks[::-1] #reversing a list

Out[5]: [11, 9, 21, 26, 33, 12, 34]

In [6]: marks[::3]

Out[6]: [34, 26, 11]

Using slicing for list modification


In [7]: list1=["abhinav","aditya","ayush","bhavya","chitra"]
print(list1)

['abhinav', 'aditya', 'ayush', 'bhavya', 'chitra']

In [8]: list1[3]="bhavika"

In [9]: list1

Out[9]: ['abhinav', 'aditya', 'ayush', 'bhavika', 'chitra']

In [11]: list1[1:4]=['Ram','Shyam']

In [12]: list1

Out[12]: ['abhinav', 'Ram', 'Shyam', 'chitra']

List functions and methods


len()
In [14]: L=[1,2,3,55,1,2,56,7]
print(len(L))

list()

append()
In [1]: L=[10,20,30]
L.append(40)
print(L)

[10, 20, 30, 40]

extend()
In [4]: x=[11,55,77,99]
x.extend([100,200]) #here data is passed in list form
print(x)

[11, 55, 77, 99, 100, 200]

insert()
In [5]: L=[10,20,30,40,50]
L.insert(3,100) #here 3 is index, 100 is value to be inserted
print(L)

[10, 20, 30, 100, 40, 50]

In [6]: L.insert(len(L),200)
print(L)

[10, 20, 30, 100, 40, 50, 200]


pop()
In [ ]: L=[10,20,30,40,50]
L.pop()
print(L)

In [8]: L=[10,20]
L.pop()
L.pop()
print(L)

[]

In [9]: L.pop()

-------------------------------------------------------------------------
--
IndexError Traceback (most recent call las
t)
<ipython-input-9-a522295e58ee> in <module>()
----> 1 L.pop()

IndexError: pop from empty list

remove()
In [10]: L=[10,20,30,40,50]
L.remove(30)
print(L)

[10, 20, 40, 50]

In [14]: L=[10,20,30,40,50,30,100]
L.remove(30)
print(L)

[10, 20, 40, 50, 30, 100]


In [15]: L=[10,20,30,40,50,30,100]
L.remove(80)
print(L)

-------------------------------------------------------------------------
--
ValueError Traceback (most recent call las
t)
<ipython-input-15-894e20f92058> in <module>()
1 L=[10,20,30,40,50,30,100]
----> 2 L.remove(80)
3 print(L)

ValueError: list.remove(x): x not in list

reverse
In [2]: L1=[10,20,30,40]
L2=[11,3,5,12,1]
L1.reverse()
print(L1)

[40, 30, 20, 10]

In [3]: L3=L2.reverse()
print(L3)

None

In [4]: print(L2)

[1, 12, 5, 3, 11]

In [5]: items=[10,20,30,40,50,60]
del items[2]
print(items)

[10, 20, 40, 50, 60]

In [6]: items=[10,20,30,40,50,60]
del items[2:5]
print(items)

[10, 20, 60]


In [8]: items=[10,20,30,40,50,60]
print(items)
del items
print(items)

[10, 20, 30, 40, 50, 60]

-------------------------------------------------------------------------
--
NameError Traceback (most recent call las
t)
<ipython-input-8-a6831a2a447a> in <module>()
2 print(items)
3 del items
----> 4 print(items)

NameError: name 'items' is not defined

Making true copy of a list


In [9]: a=[10,30,50,70,90,110]
b=a
print(a)
print(b)

[10, 30, 50, 70, 90, 110]


[10, 30, 50, 70, 90, 110]

In [10]: a[2]=88
print(a)

[10, 30, 88, 70, 90, 110]

In [11]: print(b)

[10, 30, 88, 70, 90, 110]

In [12]: a=[10,30,50,70,90,110]
b=list(a)
print(a)
print(b)

[10, 30, 50, 70, 90, 110]


[10, 30, 50, 70, 90, 110]

In [13]: a[1]=11
print(a)

[10, 11, 50, 70, 90, 110]


In [14]: print(b)

[10, 30, 50, 70, 90, 110]

min() function
In [2]: L1=[33,10,1,44,23,100]
print(min(L1))

In [4]: L1=[33,23,100,"ip","cs"]
print(min(L1))

-------------------------------------------------------------------------
--
TypeError Traceback (most recent call las
t)
<ipython-input-4-9693ebc691c6> in <module>()
1 L1=[33,23,100,"ip","cs"]
----> 2 print(min(L1))

TypeError: '<' not supported between instances of 'str' and 'int'

In [7]: L1=[33,23,100,11.5,23.6,-10,-40]
print(min(L1))

-40

In [8]: x=1
L1=[x,33,23]
print(min(L1))

max() method
In [9]: L1=[33,10,1,44,23,100]
print(max(L1))

100
sum() method
In [10]: L1=[33,10,1,44,23,100]
print(sum(L1))

211

In [11]: L1=[1,2,3]
print(sum(L1))

In [12]: L1=[33,-40,1.5]
print(sum(L1))

-5.5

In [13]: L1=[33,-40,1.5,"a"]
print(sum(L1))

-------------------------------------------------------------------------
--
TypeError Traceback (most recent call las
t)
<ipython-input-13-cb6513d3d9bd> in <module>()
1 L1=[33,-40,1.5,"a"]
----> 2 print(sum(L1))

TypeError: unsupported operand type(s) for +: 'float' and 'str'

WAP to find largest and smallest element in a


list
In [1]: list1=eval(input("Enter a list:"))
print("Largest element in a list:",max(list1))
print("Smallest element in a list:",min(list1))

Enter a list:[10,34,23,56,88]
Largest element in a list: 88
Smallest element in a list: 10
In [5]: list1=eval(input("Enter a list:"))
largest=list1[0] #10
for i in list1: #i=10,23,45,1,78,34
if i>largest: #10>10, 23>10, 45>23
largest=i #23, 45
print("Largest value is:",largest)
smallest=list1[0]
for i in list1:
if i<smallest:
smallest=i
print("Smallest value is:",smallest)

Enter a list:[10,23,45,1,78,34]
Largest value is: 78
Smallest value is: 1

String programs

WAP to count the number of vowels in user entered


string.
In [3]: string=input("enter your string:")
count=0
for i in string:
if i in ["a","e","i","o","u","A","E","I","O","U"]:
count=count+1
print("Count of vowels:",count)

enter your string:Ekta Garg


Count of vowels: 3

WAP to print number of occurrences of a given


alphabet in each string.
In [9]: string=input("enter your string:")
alpha=input("Enter an alphabet:")
count=0
for i in string:
if i==alpha:
count=count+1
print("Count of alphabet:",count)

enter your string:Informatics Pratices


Enter an alphabet:s
Count of alphabet: 2
In [6]: string=input("enter your string:")
count=0
for i in string:
if i in "aeiouAEIOU":
count=count+1
print("Count of vowels:",count)

enter your string:Ekta Garg


Count of vowels: 3

In [ ]: ​

You might also like