0% found this document useful (0 votes)
2 views

XIA2_CH10_LECT5

Chapter 6 covers list manipulation in Python, detailing methods such as reverse(), sort(), and sorted(), which modify or return lists in various ways. It also introduces functions like min(), max(), and sum() to retrieve specific values from lists. Additionally, a program example demonstrates user interaction for inserting and deleting elements from a list.

Uploaded by

gokubleachnigoi
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)
2 views

XIA2_CH10_LECT5

Chapter 6 covers list manipulation in Python, detailing methods such as reverse(), sort(), and sorted(), which modify or return lists in various ways. It also introduces functions like min(), max(), and sum() to retrieve specific values from lists. Additionally, a program example demonstrates user interaction for inserting and deleting elements from a list.

Uploaded by

gokubleachnigoi
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/ 5

Chapter 6: List Manipulation

Lecture -5

List Functions and Methods:


11. The reverse() method:
The reverse() reverses the items of the list. This is done “in place”, i.e., it does not
create a new list.
Syntax:
LIST.reverse()

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]
____________________________________________________

13. The sorted() method:


This function takes the name of the list as an argument and returns a new sorted
list with sorted elements in it.

Syntax:

Sorted(<iterable_sequence>, [reverse =False])

Example:

LIST= [90, 76, 100, 50, 43, 180, 3, 7,1200]


NEWLIST= sorted(LIST)
print(f'LIST: {LIST}\nNEWLIST: {NEWLIST}')
NEWLIST1=sorted(LIST, reverse= True)
print('NEWLIST1: {NEWLIST1}')

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:

VAL =[50, 90, 80, 17, 89, 450, 300, 270]


print(f'min({VAL}: {min(VAL)}')
print(f'max({VAL}: {max(VAL)}')
print(f'sum({VAL}: {sum(VAL)}')

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

PRG 9: Program that displays options for inserting or deleting elements in a


list. If the user chooses a deletion option, display a submenu, and
ask if element is to be deleted with value or by using its position or
a list slice is to be deleted.
print('_'*52)
LIST = [18, 54, 90, 70, 10, 40, 80]
print('The list is:', LIST)
while True:
print("------------------- Main Menu------------------- ")
print('1. Insert')
print('2. Delete')
print('3. Exit')
ch = int(input("Enter your choice 1/2/3:"))
if ch == 1:
item = int(input('Enter item:'))
pos = int(input("Insert at which position?"))
index = pos -1
LIST.insert(index, item)
print(f'Successfule! List now is : {LIST}')
elif ch == 2:
print(' ----------Deletion Menu ----------')
print('1. Delete using Value')
print('2. Delete using index')
print('3. Delete a sublist')
dch= int(input('Enter choice (1 or 2 or 3):'))

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
____________________________________________________

You might also like