0% found this document useful (0 votes)
17 views8 pages

Lists

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)
17 views8 pages

Lists

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/ 8

Page 1

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.

2. count() These methods count the elements

3. extend() Adds each element of the iterable to the end of the List

4. insert() Inserts a given element at a given index in a list.

5. pop() Removes and returns the last value from the List or the given index value.

6. remove() Removes a given object from the List.

7. reverse() Reverses objects of the List in place.

8. sort() Sort a List in ascending, descending, or user-defined order

9. copy() It returns a shallow copy of a list

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

SCE, Bengaluru Dept. of ECE Prof. Shobha.S


Page 2

list.append('orange')
print (list)

output: ['red', 'green’, ‘blue', 'orange']

Example2:

# list1
list1 = ['red', 'green’, ‘blue']
#list2
list2 = ['orange', 'violet', 'yellow']
# Add list2 to the list1
list1.append(list2)
print (list1)

output: ['red', 'green', 'blue', ['orange', 'voilet', 'yellow']]


2) extend()
Example1:

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)

output: ['red', 'green’, ‘blue', 'orange', 'voilet']


Example2:
Use extend() in Python to add elements from Set to List.

#Create list
list = ['red', 'green’, ‘blue']
#Create a set
s={'orange','voilet'}
list.extend(s)
print (list)

output: ['red', 'green’, ‘blue', 'orange', 'voilet']

3) sort()
Example1:

#list

SCE, Bengaluru Dept. of ECE Prof. Shobha.S


Page 3

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)

output: ['blue', 'green', 'orange', 'red']


------------------------------------------------------------------------------------------------------------
4) pop()
Example1:

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:

# removes 'e' from list2


list2 = [ 'a', 'b', 'c', 'd' ]

list2.remove('e')
print(list2)
output: ['a', 'c', 'd']
6) count()

Example1:

list2 = ['a', 'a', 'a', 'b', 'b', 'a', 'c', 'b']

SCE, Bengaluru Dept. of ECE Prof. Shobha.S


Page 4

print(list2.count('b'))

output: 3

7) insert()
Example1:

list = ['orange', 'blue','green']


list.insert(1, "voilet")
print(list)

output: ['orange', 'voilet', 'blue', 'green']


8) copy()
Example1:
list = ['orange', 'blue','green']
L1=list.copy()
print(L1)

output: ['orange', 'blue', 'green']


9) clear()
Example1:
list = ['orange', 'blue','green']
list.clear()
print(list)

output: []

10) reverse()
list = [1,57,93,3,45]
list.reverse()
print(list)

output: [45, 3, 93, 57, 1]

Q2. Explain list operations (concatenation and Replication) in python with an example. (4
marks)
List operations

(1) List concatenation (+)


Example:
a = [1, 2, 3]
b = [4, 5, 6]
c=a+b
print(c)
[1, 2, 3, 4, 5, 6]

(2) List repetition (*)


Example1:

[0] * 4

SCE, Bengaluru Dept. of ECE Prof. Shobha.S


Page 5

[0, 0, 0, 0]

Example2:

x=[1, 2, 3]
y=x*3
print(y)
[1, 2, 3, 1, 2, 3, 1, 2, 3]

Q3. Explain list handling functions in python with example. (8 marks)


List Functions
1) len()
2) max()
3) min()
4) sum()
nums = [3, 41, 12, 9, 74, 15]
print(len(nums))
output:6
print(max(nums))
output:74
print(min(nums))
output:3
print(sum(nums))
output:154
print(sum(nums)/len(nums))
output:25
Q4. What is Lists? Explain list slicing with example. (6 marks)
Python Lists are dynamically sized arrays, used to store collections of data.
List slicing
Syntax:
Lst[ Initial : End : IndexJump ]
Index 0 1 2 3 4
positive
Elements 20 10 30 90 80
Index
Negative -5 -4 -3 -2 -1

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]

SCE, Bengaluru Dept. of ECE Prof. Shobha.S


Page 6

----------------------------------------------------------------------------------------------------------------------
Q5. Differentiate pop and remove methods on lists. How to delete more than one element from
a list. (6 marks).

remove() pop()

remove() is a method. pop() is a method.

To delete value this method uses the value This method also uses the index as a
as a parameter. parameter to delete.

The remove() method doesn’t return any


pop() returns deleted value.
value.
Example: l=[10, 3, 25, 45]
Example: l=[10, 3, 25, 45]
l.pop(2)
l.remove()
output: 25
output:

At a time it deletes only one value from the


At a time it deletes only one value from the list.
list.

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.

# program to demonstrate use of del keyword to remove multiple elements


# assign list
num = [1, 2, 3, 2, 3, 4, 5]
# use del
del num[1:5]
# display list
print(num)
output: [1, 4, 5]

Q5. Explain list traversing with an example. (4 marks)


# using for loop
Example1:
list=[1, 2 ,3, 4]
for i in list:
print(i)
output: 1
2
3
4
5
Example2:
list=[1,2,3,4,5]
for i in range(len(list)):
print(list[i])

SCE, Bengaluru Dept. of ECE Prof. Shobha.S


Page 7

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]

# Any letters from the middle


index=int(len(list)/2)
l2=list[index-1:index+2]

#5th letters to the end of the list


l3=list[5:]

print(l1)
print(l2)
print(l3)

output: ['a', 'b', 'c']


['e', 'f', 'g']
['e', 'f', 'g', 'h', 'i', 'j']

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)

SCE, Bengaluru Dept. of ECE Prof. Shobha.S


Page 8

print("\nproduct of odd Numbers is", prod)


output: Enter the value of N 5
enter the number 8
enter the number 4
enter the number 3
enter the number 5
enter the number 7
Sum of Even Numbers is 12
product of odd Numbers is 105
P3: Implement a python program using lists to store and display the average of N integers
accepted from the user.
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
for i in range(N):
if nums[i]%2 == 0:
sum = sum + nums[i]

print("\nSum of Even Numbers is", sum)


print("\n average is", sum/N)
output: Enter the value of N 5
enter the number 2
enter the number 4
enter the number 5
enter the number 9
enter the number 10
Sum of Even Numbers is 16
average is 3.2

SCE, Bengaluru Dept. of ECE Prof. Shobha.S

You might also like