thCs -Ma Paguams
In [1]: # Python program to find largest
# number in a list
#list of numbers
list1 [10, 20, 4, 45, 99]
#sorting the list
listi.sort()
#printing the last element
orint("Largest element is: ", list1[-1])
Largest element is: 99
In [2]: # Python program to find Largest
# number in a list
#list of numbers
listi [10, 20, 4, 45, 99]
# printing the maximum element
print("Largest element is:", max(list1))
Largest element is: 99
In [3]:#Python program to find Largest
# number in a list
# creating empty list
listi [
#asking number of elements to put in list
num =
int(input( "Enter number of elements in list: "))
# iterating till num to append elements in list
for i in range(1, num + 1):
ele int (input("Enter elements: "))
listi.append (ele)
# print maximum element
print("Largest element is:", max(list1))
Enter number of elements in list: 4
Enter elements: 40
Enter elements: 50
Enter elements: 60
Enter elements: 70
Largest element is: 70
In [4]:# Python program to find smallest
# number in a lisst
# list of numbers
listi [10, 20, 4, 45, 99]
# sorting the list
list1.sort()
# printing the first element
print("Smallest element is:", *list1[:1])
Smallest element is: 4
# Python program to find smallest
Dnte
In
[5]:4 number in a list
#list of numbers
listi [10, 20, 1, 45, 99]
#printing the maximum element
print("Smallest element is:", min(1isti))
Smallest element is: 1
In [6] wolven a list, write a
Python program to Swap and last element of the list.
# first
Python3 program to swap first
#and Last element
of a list
# Swap function
def swaplist (newList) :
size len (newList)
# Swapping
temp newList[0]
newList [0] =
newlist [size - 1]
newList[size 1J = temp
return newlist
#Driver code
newList [12, 35, 9, 56, 24]
print (swapList (newList))
[24, 35, 9, 56, 12]
n tist in Python and provided
17 #oiven a
# Python3 program to swap elements
the positions of the elements, write a
program
# at given positions
# Swap function
def swapPositions(list, pos1, pos2):
list[pos1], list[pos2] list[pos2], 1ist[pos1]
return list
# Driver function
List [23, 65, 19, 90]
pos1, pos2 = 1, 3
print (swapPositions (List, pos1-1, pos2-1))
[19, 65, 23, 90]
In [8]:
The list is : [1, 4, 5, 7, 8]
5
Length of list using naive method is:
In [9]: # Python program to demonstrate working
# of Len()
a [1
a.append( "Hello")
a.append("Geeks")
a.append("For")
a.append( "Geeks")
is: ", len(a))
print("The length of list
4
The length of list is:
In [10]: elements in list
*Python program to find sum of
total = 0
# creating a list
list1 =
[11, 5, 17, 18, 23]
# Iterate each element in list
# and add them in variable total
for ele in range(0, len(1ist1)):
total total 1ist1[ele]
# printing total value
print("Sum of all elements in given list: ", total)
Sum of all elements in given 1ist: 74
In [ 1 1 ] : # Python program t o multiply all values in the
#list using traversal
def multiplyList (myList):
# Multiply elements one by one
result = 1
for x in myList:
result = result * x
return result
# Driver code
list1 [1, 2, 3]
list2 = [3, 2, 4]
print (multiplylist (list1))
print (multiplyList (list2))
24
In [13]: # Python3 code to demonstrate
#Remove empty List from List
# using list comprehension
# Initializing list
test_list =
[5, 6, [1, 3, [], [, 9]
# printing original list
print("The original list i s : " +str(test_list))
# Remove empty List from List
# using list comprehension
res [ele for ele in test_list if ele l= []]
# printing resultt
print ("List after empty list removal "* str(res))
The original list is: [5, 6, [l, 3, [], [l, 9]
List after empty list removal: [5, 6, 3, 9]
In [14]: # Python program to sort
# one list using
# the other List
def sort_list(listi, list2):
zipped_pairs = zip(list2, list1)
z [x for x in sorted(zipped_pairs)]
return z
driver code
"f", "g", "h", "i"]
x ["a", "b", "c", "d",
2
2, , 1]
y = [ 0, 1, 1, , 1
print(sort _list (x, y))
"e","k", "S"]
"e",
"e", "e", "k", "sS" "f", "o", "r", *8",
x ["", 2, 2, , 1]
y [ 0, 1, 1 , ,
1, , 1,
print (sort list(x, y))
', 'k', 'r', 'e', 'e', '8', 's',