0
2.6kviews
Write a python program to find the largest element in the list.
1 Answer
0
214views

Explanation:-

  • Create a variable n which stores the no of elements user wants to add to the list.
  • Iterate for loop n times and take the elements entered from the user and append them to a list l & pass on this list l to function max.
  • Initially first element is considered as the largest element of the list.
  • Next step is to iterate over the list l & simultaneously compare the 1st element with the elements of the list if the element is largest then swap the element with the 1st element if not then move towards next element of the list
  • Repeat this process until all the elements of the list are visited once.

Code:-

def max(l):
    maximum = l[ 0 ]
    for a in l:
        if a > maximum:
            maximum = a
    print("The largest no in the list",maximum)

l= []
ele=0
n = int(input("Enter number of elements : "))
for i in range(0, n):
    print("Enter elements for the list:")
    ele = int(input()) 
    l.append(ele)
print(l)
max(l)

Output:-

enter image description here

Please log in to add an answer.