XIA2_CH10_LECT5
XIA2_CH10_LECT5
Lecture -5
The reserve() takes no argument, returns no list; reverses the list ‘in place’ and
does not return anything.
Example:
LIST1=list('WELCOME')
print(f'LIST1 Sidplay as:\n{LIST1}')
LIST1.reverse()
print(LIST1)
print(f'LIST1 in reverse order display as:\n {LIST1}')
LIST2=[50, 70, 150, 70, 98,150]
print(f'LIST2 Sidplay as:\n{LIST2}')
LIST2.reverse()
print(f'LIST2 in reverse order display as:\n{LIST2}')
Output:
LIST1 Sidplay as:
['W', 'E', 'L', 'C', 'O', 'M', 'E']
['E', 'M', 'O', 'C', 'L', 'E', 'W']
LIST1 in reverse order display as:
['E', 'M', 'O', 'C', 'L', 'E', 'W']
LIST2 Sidplay as:
[50, 70, 150, 70, 98, 150]
LIST2 in reverse order display as:
[150, 98, 70, 150, 70, 50]
12. The sort() method:
The sort() function sorts the items of the list, by default in increasing order. This is
done “in place”, i.e., it does not create a new list.
Syntax:
LIST.sort([<reverse = False/True>])
print('_'*52)
LIST1=list('WELCOME')
LIST2=[20, 90, 170, 80, 99, 75, 20]
print(f'LIST1 display as:\n{LIST1}')
print(f'LIST2 display as:\n{LIST2}')
print('_'*52)
LIST1.sort()
LIST2.sort()
1
print(f'LIST1 and LIST2 in ascending order display as:')
print(f'LIST1: {LIST1} \nLIST2: {LIST2}')
print('_'*52)
LIST3=[90, 3, 120, 7, 89,150]
print(f'LIST3 display as: {LIST3}')
print('_'*52)
LIST3.sort(reverse=True)
print(f'LIST3 in descending order display as:\n{LIST3}')
print('_'*52)
Output:
____________________________________________________
LIST1 display as:
['W', 'E', 'L', 'C', 'O', 'M', 'E']
LIST2 display as:
[20, 90, 170, 80, 99, 75, 20]
____________________________________________________
LIST1 and LIST2 in ascending order display as:
LIST1: ['C', 'E', 'E', 'L', 'M', 'O', 'W']
LIST2: [20, 20, 75, 80, 90, 99, 170]
____________________________________________________
LIST3 display as: [90, 3, 120, 7, 89, 150]
____________________________________________________
LIST3 in descending order display as:
[150, 120, 90, 89, 7, 3]
____________________________________________________
Syntax:
Example:
Output:
LIST: [90, 76, 100, 50, 43, 180, 3, 7, 1200]
NEWLIST: [3, 7, 43, 50, 76, 90, 100, 180, 1200]
NEWLIST1: {NEWLIST1}
2
14. The min(), max(), sum() functions:
The min(), max() and sum() functions takes the list sequence and return the minimum
element, maximum element and sum of the elements of the list respectively.
min (<list>)
max (<list>)
sum (<list>)
Where the <list> is a list of elements on which these functions will work.
Exampe:
Output:
min([50, 90, 80, 17, 89, 450, 300, 270]: 17
max([50, 90, 80, 17, 89, 450, 300, 270]: 450
sum([50, 90, 80, 17, 89, 450, 300, 270]: 1346
3
if dch == 1:
item = int(input('Enter item of item to be deleted:'))
LIST.remove(item)
print(f'List now is: {LIST}')
elif dch == 2:
index =int(input("Enter index of item to be deleted:"))
LIST.pop(index)
print("List now is :", LIST)
elif dch == 3:
lower =int(input("Enter lower limit of list slice to be deleted:"))
upper=int(input("Enter upper limit of list slice to be deleted:"))
del LIST[lower:upper]
print("List now is :",LIST)
else:
print("Valid choices are 1/2/3 only.")
elif ch == 3:
break;
else:
print("Valid choices are 1/2/3/ only.")
print('_'*52)
Output:
____________________________________________________
The list is: [18, 54, 90, 70, 10, 40, 80]
------------------- Main Menu-------------------
1. Insert
2. Delete
3. Exit
Enter your choice 1/2/3:1
Enter item:1000
Insert at which position?4
Successfule! List now is : [18, 54, 90, 1000, 70, 10, 40, 80]
------------------- Main Menu-------------------
1. Insert
2. Delete
3. Exit
Enter your choice 1/2/3:2
----------Deletion Menu ----------
1. Delete using Value
2. Delete using index
3. Delete a sublist
Enter choice (1 or 2 or 3):1
Enter item of item to be deleted:10
List now is: [18, 54, 90, 1000, 70, 40, 80]
------------------- Main Menu-------------------
1. Insert
2. Delete
3. Exit
4
Enter your choice 1/2/3:2
----------Deletion Menu ----------
1. Delete using Value
2. Delete using index
3. Delete a sublist
Enter choice (1 or 2 or 3):2
Enter index of item to be deleted:5
List now is : [18, 54, 90, 1000, 70, 80]
------------------- Main Menu-------------------
1. Insert
2. Delete
3. Exit
Enter your choice 1/2/3:2
----------Deletion Menu ----------
1. Delete using Value
2. Delete using index
3. Delete a sublist
Enter choice (1 or 2 or 3):3
Enter lower limit of list slice to be deleted:2
Enter upper limit of list slice to be deleted:5
List now is : [18, 54, 80]
------------------- Main Menu-------------------
1. Insert
2. Delete
3. Exit
Enter your choice 1/2/3:3
____________________________________________________