Lists
Lists
Module-2
Q1. Explain List operations and methods in python.” lists are mutable” Explain this statement
with example. (8 marks)
Explain list handling functions in python with example. (8 marks)
Describe any two list methods and operations in python. (4 marks)
Lists are mutable
Lists in Python are mutable data types as the elements of the list can be modified, individual
elements can be replaced, and the order of elements can be changed even after the list has been
created.
Example:
list = ['orange', 'blue', 'green', 'purple']
list[2]= "voilet"
print(list)
output: ['orange', 'blue', 'voilet', 'purple']
List Methods in Python
Sl. Method Description
no
1. append() Used for appending and adding elements to the end of the List.
3. extend() Adds each element of the iterable to the end of the List
5. pop() Removes and returns the last value from the List or the given index value.
10. clear() This method is used for removing all items from the list.
1) append()
Example1:
#Create list
list = ['red', 'green’, ‘blue']
# Add 'orange' to the list
list.append('orange')
print (list)
Example2:
# list1
list1 = ['red', 'green’, ‘blue']
#list2
list2 = ['orange', 'violet', 'yellow']
# Add list2 to the list1
list1.append(list2)
print (list1)
list = [1, 2, 3]
list.extend([4, 5, 6])
print(list)
output: [1, 2, 3, 4, 5, 6]
Example2:
Use extend() in Python to add elements from Tuple to List.
#Create list
list = ['red', 'green’, ‘blue']
#create a tuple
t=('orange','voilet')
list.extend(t)
print (list)
#Create list
list = ['red', 'green’, ‘blue']
#Create a set
s={'orange','voilet'}
list.extend(s)
print (list)
3) sort()
Example1:
#list
numbers = [1, 3, 4, 2]
#sort the numbers
print(numbers.sort())
print(numbers)
output: [1, 2, 3, 4]
Example2:
# Creating List
strs = ["blue", "orange", "red", "green"]
# using sort() methods
strs.sort()
print(strs)
list1 = [1, 2, 3, 4, 5, 6]
# Pops and removes the last element from the list
print (list1.pop())
print(list1)
output: 6
[1, 2, 3, 4, 5]
Example2:
list1 = [1, 2, 3, 4, 5, 6]
# Pops and removes the 0th index element from the list
print (list1.pop (0))
print (list1)
output: 1
[2, 3, 4, 5, 6]
5) remove()
Example1:
list2.remove('e')
print(list2)
output: ['a', 'c', 'd']
6) count()
Example1:
print(list2.count('b'))
output: 3
7) insert()
Example1:
output: []
10) reverse()
list = [1,57,93,3,45]
list.reverse()
print(list)
Q2. Explain list operations (concatenation and Replication) in python with an example. (4
marks)
List operations
[0] * 4
[0, 0, 0, 0]
Example2:
x=[1, 2, 3]
y=x*3
print(y)
[1, 2, 3, 1, 2, 3, 1, 2, 3]
Example1:
# Initialize list
Lst = [20,10,30,90,80]
# Display list
print(Lst[: :])
output: [20, 10, 30, 90, 80]
# Initialize list
Example2:
Lst = [20,10,30,90,80]
# Display list
print(Lst[-5:-2:1])
Output: [20, 10, 30]
----------------------------------------------------------------------------------------------------------------------
Q5. Differentiate pop and remove methods on lists. How to delete more than one element from
a list. (6 marks).
remove() pop()
To delete value this method uses the value This method also uses the index as a
as a parameter. parameter to delete.
It throws value error in case of value It throws index error in case of an index
doesn’t exist in the list. doesn’t exist in the list.
output: 1
2
3
4
5
Programs on lists:
P1: Make a list of ten letters of the alphabet then using the slice operation do the following:
i) print the first three letters from the list
ii) Print any letters from the middle
iii) Print the letters from 5th letters to the end of the list
# Create list of 10 letters
list=['a','b','c','d','e','f','g','h','i','j']
# First three letters from the list
l1=list[:3]
print(l1)
print(l2)
print(l3)
P2: Write a python program to accept ‘N’ numbers from user, find sum of all even numbers
and product of all odd numbers in entered list.
# Create empty list
nums = []
N=int(input("Enter the value of N "))
for i in range(N):
val = int(input('enter the number'))
nums.append(val)
sum = 0
prod = 1
for i in range(N):
if nums[i]%2 == 0:
sum = sum + nums[i]
else:
prod= prod*nums[i]
print("\nSum of Even Numbers is", sum)
sum = 0
for i in range(N):
if nums[i]%2 == 0:
sum = sum + nums[i]