Python Program to find the Largest Number in a List
What is a List in Python:
The order of different data types is stored in a list in Python. A list is a grouping of values or things of various kinds. Because Python lists are mutable, we can change an element's state after it has been created. A comma (,) is used to separate the list's items, and square brackets enclose them.
A list, a sequence type, is used to store data collection. The list is the most widely used and reliable form, even though Python has six data types that can hold sequences. Tuples and String are similar sequence data formats.
Python lists are the same as dynamically scaled arrays defined in other languages, like Java's Array List and C++'s vector. Using symbols [] and commas to separate each item, a list is a collection of items.
CODE:
# a simple list
list1 = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
list2 = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
# printing the list
print(list1)
print(list2)
# printing the type of list
print(type(list1))
print(type(list2))
OUTPUT:
['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
<class 'list'>
<class 'list'>
CODE EXPLANATION:
This code creates two lists, list1, and list2. List 1 contains the names of eight planets, while list 2 contains the names of seven days of the week. The code then uses the print() function to display the contents of both lists. The contents of list 1 are displayed first, followed by list 2. Finally, the code uses the print() function to display the data type of both lists. In this case, both lists are of type list, a built-in data type in Python for storing ordered collections of items. This code demonstrates how to create Python lists and display their contents and data type.
Characteristics of List:
- Lists are organized.
- The list's element can be accessed by index.
- The mutable type includes lists.
- The list types are mutable.
- The number of different elements can be stored in a list.
Checking for Ordered List:
CODE:
a = [ "raju", 21, "k", 2.50, "Rahul", 5, 6 ]
b = [ 12, 32, 16, "l", 6.50, "Rahul", 6 ]
a == b
OUTPUT:
False
CODE EXPLANATION:
This code compares two lists, a and b, using the equality operator ‘==’. The equality operator ‘==’ returns True if the values of the two operands are equal and False otherwise. In this case, the lists a and b contain different elements, so the comparison of a == b will return False.
Although the elements in both lists were the same, the second list changed the fifth element's index position, which went against the lists' intentional order. The comparison between the two lists yields false.
CODE:
a = [ 11, 22, "Raju", 2.70, "Rakul", 56, 60]
b = [ 11, 22, "Raju", 2.70, "Rakul", 56, 60]
a == b
OUTPUT:
True
CODE EXPLANATION:
This code defines lists a and b and assigns the same values. The expression a == b compares the elements of the two lists and returns a Boolean value of True if all elements of the lists are equal in value and order. So, the result of a == b will be True, indicating that the elements in the two lists are equal.
Lists maintain the order of the elements permanently. This is why it is a planned gathering of things.
Types of Methods to Find the Largest Number in a list:
Using the built-in max() function:
Using a user-defined input:
CODE:
numbers = list(map(int,input("Enter the List Elements:").split()))
largest = max(numbers)
print("The Largest Number is:",largest)
OUTPUT:
Enter the List Elements:-2 1 4 7 3 9
The Largest Number is: 9
CODE EXPLANATION:
First, the input() function is used to prompt the user to enter a list of integers separated by spaces. The split() method is used to split the user input string into a list of strings, each representing an integer. The map() function is used to apply the int() function to each element of the list of strings, converting them to integers. The resulting list of integers is assigned to the numbers variable. Next, the max() function is used to find the largest number in the numbers list, and the result is assigned to the largest variable. Finally, the print() function is used to display a message that includes the largest number found. The message is constructed using string concatenation, where the str() function is used to convert the largest variable to a string that can be concatenated with the message text.
Using a for loop to iterate through the list and compare each number with the current largest:
CODE:
numbers = [3, 5, 2, 8, 1]
largest = numbers[0]
for number in numbers:
if number > largest:
largest = number
print("The Largest Number is:",largest)
OUTPUT:
The Largest Number is: 8
CODE EXPLANATION:
This code finds the largest number in a given list of integers. First, a list of integers is defined and assigned to the number variable. Next, the largest variable is initialized with the first number in the list, which is numbers[0]. Then, a for loop is used to iterate through each element in the numbers list. For each element, the code checks if it is greater than the current largest number (largest). If it is, then the largest variable is updated with the value of the current element. Once the loop has finished iterating through all the elements in the list, the largest variable contains the largest number in the list.
Finally, the print() function is used to display a message that includes the largest number found. The message is constructed using string concatenation, where the str() function is used to convert the largest variable to a string that can be concatenated with the message text.
Using the sorted() function:
To sort the list in descending order and then select the first element:
CODE:
nums = list(map(int,input("Enter List Elements:").split()))
sorted_nums = sorted(nums, reverse=True)
greater = sorted_numbers[0]
print("The Largest Number is:",greater)
OUTPUT:
Enter List Elements:-2 6 7 1 9 -32 12
The Largest Number is: 12
CODE EXPLANATION:
This code finds the largest number in a given list of integers using the sorted() function. First, the input() function is used to prompt the user to enter a list of integers separated by spaces. The split() method is used to split the user input string into a list of strings, each representing an integer. The map() function is used to apply the int() function to each element of the list of strings, converting them to integers. The resulting list of integers is assigned to the nums variable. Next, the sorted() function is used to sort the nums list in descending order, i.e., from largest to smallest. The resulting sorted list is assigned to the sorted_nums variable. Then, the greater variable is initialized with the first number in the sorted_nums list, which is sorted_nums[0], i.e., the largest number in the list. Finally, the print() function is used to display a message that includes the largest number found.
Using the reduce() function:
From the functools module to continuously compare pairs of numbers in the list until the largest number is found:
CODE:
from functools import reduce
numbers = [1,2,3,4,-1,-2,7]
greater = reduce(lambda a, b: a if a > b else b, numbers)
print("The Largest Number is:",greater)
OUTPUT:
The Largest Number is: 7
CODE EXPLANATION:
First, a list of integers is defined and assigned to the number variable. Then, the reduce() function is used to apply a lambda function to each element of the numbers list. The lambda function takes two arguments, a and b, which represent the current accumulated value and the current element of the list, respectively. For each element, the lambda function checks if it is greater than the current accumulated value (a). If it is, then the lambda function returns the current element (b) as the new accumulated value. If it is not, then the lambda function returns the current accumulated value (a) as the new accumulated value. Once the reduce() function has finished iterating through all the elements in the list, the greater variable contains the largest number in the list. Finally, the print() function is used to display a message that includes the largest number found.
The same method using a user-defined input:
CODE:
from functools import reduce
nums = list(map(int,input("Enter List Elements:").split()))
greater = reduce(lambda a, b: a if a > b else b, nums)
print("The Largest Number is:",greater)
OUTPUT:
Enter List Elements:9 4 1 8 3 2 7
The Largest Number is: 9
CODE EXPLANATION:
This code finds the largest number in a given list of integers using the reduce() function from the functools module. The user is prompted to enter a list of integers separated by spaces, which are converted to a list of integers using the map() function. The reduce() function applies a lambda function to each element of the nums list and returns the largest element using a conditional expression. Finally, the print() function is used to display a message that includes the largest number found.