CH 9 Lists
CH 9 Lists
LISTS
1. Creating Lists
To create a list, enclose the elements in square brackets and separate with commas.
Syntax:
list_name=[item1,item2…….item n]
eg: list1=[1.2.”python”,5]
2. Accessing Lists
The values stored in a list can be accessed using the slice operator ([ ],[:]) with indexes.
List_name[start:end] will give you the elements between indices start and end.
Eg. L1=[1,2,3,4,5]
>>>L1[3]
4
>>>L1[8]
Index error: List index out of range
>>>L1[-3]
3
>>>L1[2]=6 # assigning value
>>>L1
[1,2,6,4,5]
Traversing a list
Traversal of list means accessing and processing each element of it.
❖ Using for loop
Syntax:
eg: L=[‘p’,’y’,’t’,’h’,’o’,’n’]
for a in L:
print(a)
Output:
P
y
t
h
o
n
Output:
Enter the elements: Monday
M
o
n
d
a
y
M
o
n
d
a
y
❖ Using while loop
>>>d=list(input(“Enter the elements:”))
Enter the elements: Monday
>>>i=0
while i<len(d):
print(d[i])
i+=1
Output:
M
o
n
d
a
y
List Operators
1. Concatenation operator(+)
2. Repetition Operator (*)
3. Slice Operator([ ] and [:])
4. Relational Operator (<, <=,>, >=.!=,==)
5. Membership Operator(in and not in)
3. Slice Operator
a. List_name[start,stop]
This syntax will create a list having elements of list on indexes from start to stop-1.
Eg. >>>L1=[12,56,87,45,23,97,56,27]
>>>L1[2:-2]
[87,45,23,97]
[87,45,23,97,56]
>>>L1[-1:-6] -1 > -6
[] # empty list
>>>L1[0:len(L1)]
[12,56,87,45,23,97,56,27]
b. list_name [start:end:step]
This syntax will give you elements between indices from start to end – 1
with skipping elements as per the value of step.
[56,45,97]
[27,56,97,23,45,87,56,12]
>>>L1
Eg. >>>L1[2:4]=[“computer”]
>>>L1
12.56,” computer”,23,97,56,27]
The values being assigned must be a sequence i.e, a list or string or tuple etc.
Eg. >>>L1=[1,2,3]
>>>L1
[1,2,’6’,’0’,’4’]
>>>L1[1:3]=[345]
>>>L1
[1,345]
>>>L1[10:20]=”abcd” # no error though list slice limits are outside the length
Eg.>>> L1==L2
Eg. >>>L1=[10,20,30,40]
>>> 20 in L1
True
>>> 50 in L1
False
>>>20 not in L1
False
>>>60 not in L1
True
7. index()
syntax: listname.index(element)
Returns index of the first occurrence of the element in the list. If the element is not present,
ValueError is generated
>>> list1.index(20)
>>> list1.index(90)
8. remove()
Syntax: list_name.remove(element)
Removes the given element from the list. If the element is present multiple times, only the first
>>> list1.remove(30)
>>> list1
>>> list1.remove(90)
9. pop()
Syntax: list_name.pop(index)
Returns the element whose index is passed as parameter to this function and also removes it from the
list. If no parameter is given, then it returns and removes the last element of the list
>>> list1.pop(3)
40
>>> list1
[10, 20, 30, 50, 60]
>>> list1 = [10,20,30,40,50,60]
>>> list1.pop()
60
10. reverse()
Syntax: list_name.reverse()
>>> list1.reverse()
>>> list1
>>> list1.reverse()
>>> list1
>>>L1=[10,20,30,40,50]
>>>del L1[2]
>>>L1
[10,20,40,50]
>>>del L1[0:3]
>>>L1
[50]
>>>del L1
>>>L1
Note: Whereas pop() and remove() provide empty list([ ]) after all the elements are removed or
popped out.
12. sort()
Syntax: List_name.sort()
Sorts the elements of the given list in-place (by default: Ascending order)
>>>list1 = ['Tiger','Zebra','Lion',
>>> list1.sort()
>>> list1
'Tiger', 'Zebra']
>>> list1 = [34,66,12,89,28,99]
>>> list1
[99,89,66,34,28,12]
13. sorted()
Syntax: sorted(list_name)
It takes a list as parameter and creates a new list consisting of the same elements arranged in
sorted order
>>> list1
>>> list2
14. min()
syntax: min(list_name)
>>> min(list1)
12
15. max()
Syntax: max(list_name()
92
16. sum()
Syntax: sum(list_name)
>>> sum(list1)
284
Nested List
>>> list1 = [1,2,'a','c',[6,7,8],4,9] #fifth element of list is also a list
>>> list1[4]
[6, 7, 8]
To access the element of the nested list of list1, we have to specify two indices
Syntax: list1[i][j]
The first index i will take us to the desired nested list and second index j will take us to the
desired element in that nested list.
Eg. >>> list1[4][1]
7
Copying List
Simplest way to copy list is to assign it to another list.
Eg; >>>L1=[1,2,3]
>>>L2=L1
>>>L2
[1,2,3]
If any change made to L1 will reflect in L2 also. Therefore, any changes made to any of the lists
will reflect in both lists.
>>>L1[2]=4
>>>L1
[1,2,4]
>>>L2
[1,2,4]
>>>L2[1]=0
>>>L1
[1,0,4]
Syntax:
newList = oldList[:]
eg. >>>L1=[10,20,30]
>>>L2=L1[ : ]
>>>L2
[10,20,30]
>>> L2 = list(L1)
>>> L2
[10, 20, 30, 40]
Programs in List
1. Write a program to find the largest / smallest number in a list.
L = eval(input("Enter the list: "))
Largest = L[0]
Lindex = 0
for i in range(len(L)):
if L[i] > Largest:
Largest = L[i]
Lindex = i
print("The largest number =", Largest, "at index =", Lindex)
OUTPUT:
Enter the list: [12, 45, 23, 89, 34, 23]
The largest number = 89 at index = 3
OUTPUT:
Enter the list: [23, 78, 45, 12, 90, 67]
Second largest number in the list = 78
5. WAP to input a list of numbers and swap elements at the even location with the
elements at odd location.
L=eval(input(“Enter the list elements:”))
for I in range(0,len(L),2):
L[i], L[i+1]=L[i+1],L[i]
print(“The swapped list:”,L)
OUTPUT:
Enter the list elements: [1, 2, 3, 4, 5]
The swapped list: [2, 1, 4, 3, 5]
6. Write a program to input a list of elements, search for a given element in the list.
L = eval(input("Enter the list: "))
val = int(input("Enter the element to be searched: "))
for i in range(len(L)):
if val == L[i]:
print(val, "found at index", i)
break
else:
print(val, "not found in given list")
OUTPUT:
Enter the list: [10, 20, 30, 40, 50]
Enter the element to be searched: 30
30 found at index
******************************