Unit 2 Chapter-4 Lists
Unit 2 Chapter-4 Lists
Chapter - 4
Lists
Sequences in Python
Lists, Dictionaries, Strings, Tuples and Sets
Creating a list
Lists are enclosed in square brackets [ ] and
each item is separated by a comma.
Output:
Red
Green
Blue
Yellow
Black
for i in range(len(list1)):
print(list1[i])
Output:
Red
Green
Blue
Yellow
Black
Courtesy: NCERT
List methods and built in Functions
Courtesy: NCERT
List methods and built in Functions
Courtesy: NCERT
List methods and built in Functions
Courtesy: NCERT
List methods and built in Functions
Courtesy: NCERT
List methods and built in Functions
Courtesy: NCERT
Manipulating a List
Various list manipulation methods are:
1. Append an element
2. Insert an element
3. Append a list to the given list
4. Modify an existing element
5. Delete an existing element from its position
6. Delete an existing element with a given value
7. Sort the list in the ascending order
8. Sort the list in descending order
9. Display the list.
1. Append an element
2. Insert an element
a. Slicing
b. Concatenation
c. Repetition
d. Membership testing
e. Indexing
Syntax:
list[start:stop:step]
start is the starting point.
stop is the stopping point, which is not included.
Step is the step size. It is optional. Default value is 1.
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
a. Slicing
list =['I','N','D','I','A']
>>> x='computer'
>>> x[1:4] # omp
>>> x[3:] # puter
>>> x[:5] # compu
>>> x[-3:] # ter
>>> x[:-2] # comput
#negative indexes
#elements at index -6,-5,-4,-3 are sliced
>>> list1[-6:-2]
['Green','Blue','Cyan','Magenta’]
>>> list1[::-2]
['Black', 'Magenta', 'Blue', 'Red']
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
b. Concatenation
Multiple sequences/lists can be combined together with help
of certain operators.
Python allows adding two lists using ‘+’ operator.
list1=['Red','Green']
list2=[10,20]
list1=list1+['Blue’]
print(list1) #['Red', 'Green', 'Blue']
list2=list2+[30]
print(list2) #[10, 20, 30]
list3=list1+list2
print(list3) #['Red', 'Green', 'Blue', 10, 20, 30]
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
b. Concatenation
>>> list1 = [1,2,3]
>>> str1 = "abc"
>>> list1 + str1
TypeError: can only concatenate list
(not "str") to list
x="Python"*3
print(x) Output: PythonPythonPython
e.g.1
x='Python'
print ('o' in x)
Output: True
e.g.2
y=[10,20,30,40]
print (50 in y)
Output: False
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
d. Membership Testing
The Operator not in transpose returns True
if the element is not present in the list, else it
returns False.
e.g.1
list1=['Red','Green','Blue']
list2=[10,20,30]
Output:
print(list1[-1]) Blue
print(list2[2]) 30
L=[]
n=int(input("How many items to be entered:"))
i=1
while(i<=n):
a=int(input("Enter element:"))
L.append(a)
i=i+1
print(L)
NumList = [1,33,22,88,4,44]
Number=len(NumList)
print("Length of this List is : ", Number)
smallest = largest = NumList[0]
ctr=0
List = [1,33,22,88,1,4,44,1]
print(List)
num=int(input("Enter Number in the List:"))
if(num= =List[i]):
ctr=ctr+1
L=[]
n=int(input("How many items to be enetered:"))
i=1
while(i<=n):
a=int(input("Enter element:"))
L.append(a)
i=i+1
sum=0
for i in range(0, len(L)):
sum=sum+L[i]
avg=sum/n
L=[10,30,88,27,93]
print(L)
n=int(input("Enter item to be search:"))
flag=0
for i in range(0, len(L)):
if (n==L[i]):
flag=1
break
if (flag==1):
print("Element found")
else:
print("Element not found")
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt