Chapter 9 Lists
Chapter 9 Lists
Chapter 9: Lists
1. Introduction to List
• Definition: A list is an ordered sequence of elements that is mutable. Elements can be of
different data types, including integers, floats, strings, tuples, and other lists.
• Syntax: Elements are enclosed in square brackets and separated by commas. Indices start
from 0.
Example 9.1:
[2]:
list1 = [2, 4, 6, 8, 10, 12]
list2 = ['a', 'e', 'i', 'o', 'u']
list3 = [100, 23.5, 'Hello']
list4 = [['Physics', 101], ['Chemistry', 202], ['Maths', 303]]
• list1: List of even numbers.
• list2: List of vowels.
• list3: List with mixed data types.
• list4: Nested list.
[ ]: list1[0] # returns 2
list1[3] # returns 8
list1[-1] # returns 12 (last element)
1
Chapter 9 Lists
[5]: True
5. Traversing a List
• Using for Loop:
[ ]: i = 0
while i < len(list1):
print(list1[i])
i += 1
2
Chapter 9 Lists
Experimnt all the built-in functions and methods using the examples in the table:
[ ]:
7. Nested Lists
• Definition: Lists within lists.
2. Using list():
[ ]: list2 = list(list1)
3. Using copy module:
[ ]: list2 = copy.copy(list1)
3. Using copy module:
[ ]: import copy
list2 = copy.copy(list1)
[ ]: def increment(list2):
for i in range(len(list2)):
list2[i] += 5
print(list2)
increment(list1)
3
Chapter 9 Lists
# end of function
list1 = [10,20,30,40,50] # Create a list
print("Reference of list in Main",id(list1))
print("The list before the function call")
print(list1)
increment(list1) # list1 is passed as parameter to function
print("The list after the function call")
print(list1)
Explanation: This program demonstrates how passing a list to a function affects its elements. The
increment function adds 5 to each element in the list. The list reference inside the function is the
same as the reference in the main program, showing that the function modifies the original list.
Program 9-2
# end of function
list1 = [10,20,30,40,50] # Create a list
print("ID of list before function call:",id(list1))
print("The list before function call:")
print(list1)
increment(list1) # list1 passed as parameter to function
print('\nID of list after function call:',id(list1))
print("The list after the function call:")
print(list1)
Program 9-3: Menu-Driven Program for List Operations Explanation: This program
provides a menu-driven interface to perform various list operations such as appending elements,
inserting elements at a desired position, appending another list, modifying elements, deleting ele-
ments, sorting the list, and displaying the list.
4
Chapter 9 Lists
# append element
if choice == 1:
element = int(input("Enter the element to be appended: "))
myList.append(element)
print("The element has been appended\n")
5
Chapter 9 Lists
myList.pop(pos)
print("The element has been deleted\n")
[ ]: #Program 9-4
#Function to calculate average marks of n students
def computeAverage(list1,n):
#initialize total
total = 0
for marks in list1:
#add marks to total
total = total + marks
average = total / n
return average
6
Chapter 9 Lists
for i in range(0,n):
print("Enter marks of student",(i+1),":")
marks = int(input())
#append marks in the list
list1.append(marks)
average = computeAverage(list1,n)
print("Average marks of",n,"students is:",average)
Program 9-5:
Write a user-defined function to check if a number is present in the list or not. If the number is
present, return the position of the number. Print an appropriate message if the number is not
present in the list.
[ ]: #Program 9-5
#Function to check if a number is present in the list or not
def linearSearch(num,list1):
for i in range(0,len(list1)):
if list1[i] == num: #num is present
return i #return the position
return None #num is not present in the list
#end of function
Exercises
1. Write a program to create a list of first 10 natural numbers.
7
Chapter 9 Lists
[ ]:
[ ]:
[ ]: