Python Program to Print Largest Even and Largest Odd Number in a List

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language

List in Python :

The list data type is one of the most often used data types in Python. The square brackets [ ] easily identify a Python List. Lists are used to store data items, with each item separated by a comma (,). A Python List can include data elements of any data type, including integers and Booleans.

One of the primary reasons that lists are so popular is that they are mutable. Any data item in a List can be replaced by any other data item if it is mutable. This distinguishes Lists from Tuples, which are likewise used to store data elements but are immutable.

Given a list, the task is to print the largest Even and Largest Odd number in the given list in Python

Examples:

Example1:

Input:

given list =[12, 21, 45, 146, 197, 4]

Output:

The Largest even number in the given list [12, 21, 45, 146, 197, 4] = 146
The Largest odd number in the given list [12, 21, 45, 146, 197, 4] = 197

Example2:

Input:

given list =  [532, 234, 9273, 845, 1023, 9]

Output:

The Largest even number in the given list [532, 234, 9273, 845, 1023, 9] = 532
The Largest odd number in the given list [532, 234, 9273, 845, 1023, 9] = 9273

Program to Print Largest Even and Largest Odd Number in a List in Python

Below are the ways to print the largest even element and largest odd element in the given list :

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

Method #1: Using sort() and Conditional Statements(User Input)

Approach:

  • Take the user’s input on the number of elements to include in the list.
  •  Using a for loop, Scan the elements from the user and append them to a list.
  • Using a for loop, retrieve the elements from the list one at a time, determine whether they are odd or even, and append them to various lists.
  • Sort both lists separately and calculate the length of each.
  • Output the final elements of the sorted lists.
  • Exit of Program

Below is the implementation:

# scanning the total number of elements of the given list
totalCount = int(
    input("Enter the total number of elements of the given list = "))
# Taking a empty list
given_list = []
# Using for loop to loop totalCount times
for i in range(totalCount):
    eleme = int(input("Enter some random element(integer) = "))
    given_list.append(eleme)
# Taking two empty lists which stores eeven and odd numbers
evenNumList = []
oddNumList = []
# Traversing the list using for loop
for eleme in given_list:
  # if the element is even then add this element to evenNumList using append() function
    if(eleme % 2 == 0):
        evenNumList.append(eleme)
  # if the element is even then add this element to oddNumList using append() function
    else:
        oddNumList.append(eleme)
# sorting evenNumList using sort() function
evenNumList.sort()
# sorting oddNumList using sort() function
oddNumList.sort()
print("The Largest even number in the given list",
      given_list, "=", evenNumList[-1])
print("The Largest odd number in the given list",
      given_list, "=", oddNumList[-1])

Output:

Enter the total number of elements of the given list = 6
Enter some random element(integer) = 12
Enter some random element(integer) = 21
Enter some random element(integer) = 45
Enter some random element(integer) = 146
Enter some random element(integer) = 197
Enter some random element(integer) = 4
The Largest even number in the given list [12, 21, 45, 146, 197, 4] = 146
The Largest odd number in the given list [12, 21, 45, 146, 197, 4] = 197

Method #2: Using sort() and Conditional Statements(Static Input)

Approach:

  • Give the elements of the list as static input
  • Using a for loop, retrieve the elements from the list one at a time, determine whether they are odd or even, and append them to various lists.
  • Sort both lists separately and calculate the length of each.
  • Output the final elements of the sorted lists.
  • Exit of Program

Below is the implementation:

# given list
given_list = [532, 234, 9273, 845, 1023, 9]
# Taking two empty lists which stores eeven and odd numbers
evenNumList = []
oddNumList = []
# Traversing the list using for loop
for eleme in given_list:
  # if the element is even then add this element to evenNumList using append() function
    if(eleme % 2 == 0):
        evenNumList.append(eleme)
  # if the element is even then add this element to oddNumList using append() function
    else:
        oddNumList.append(eleme)
# sorting evenNumList using sort() function
evenNumList.sort()
# sorting oddNumList using sort() function
oddNumList.sort()
print("The Largest even number in the given list",
      given_list, "=", evenNumList[-1])
print("The Largest odd number in the given list",
      given_list, "=", oddNumList[-1])

Output:

The Largest even number in the given list [532, 234, 9273, 845, 1023, 9] = 532
The Largest odd number in the given list [532, 234, 9273, 845, 1023, 9] = 9273

Method #3:Using sort and List Comprehension(Static Input)

Approach:

  • Give the elements of the list as static input
  • Take all even numbers and odd numbers of the given list into two separate lists using List Comprehension
  • Sort both lists separately and calculate the length of each.
  • Output the final elements of the sorted lists.
  • Exit of Program

Below is the implementation:

# given list
given_list = [532, 234, 9273, 845, 1023, 9]
# Take all even numbers and odd numbers of the given list into
# two separate lists using List Comprehension
evenNumList = [eleme for eleme in given_list if eleme % 2 == 0]
oddNumList = [eleme for eleme in given_list if eleme % 2 != 0]
# sorting evenNumList using sort() function
evenNumList.sort()
# sorting oddNumList using sort() function
oddNumList.sort()
print("The Largest even number in the given list",
      given_list, "=", evenNumList[-1])
print("The Largest odd number in the given list",
      given_list, "=", oddNumList[-1])

Method #4:Using sort and List Comprehension(User Input)

Approach:

  • Take the user’s input on the number of elements to include in the list.
  •  Using a for loop, Scan the elements from the user and append them to a list.
  • Take all even numbers and odd numbers of the given list into two separate lists using List Comprehension
  • Sort both lists separately and calculate the length of each.
  • Output the final elements of the sorted lists.
  • Exit of Program

Below is the implementation:

# scanning the total number of elements of the given list
totalCount = int(
    input("Enter the total number of elements of the given list = "))
# Taking a empty list
given_list = []
# Using for loop to loop totalCount times
for i in range(totalCount):
    eleme = int(input("Enter some random element(integer) = "))
    given_list.append(eleme)
# Take all even numbers and odd numbers of the given list into
# two separate lists using List Comprehension
evenNumList = [eleme for eleme in given_list if eleme % 2 == 0]
oddNumList = [eleme for eleme in given_list if eleme % 2 != 0]
# sorting evenNumList using sort() function
evenNumList.sort()
# sorting oddNumList using sort() function
oddNumList.sort()
print("The Largest even number in the given list",
      given_list, "=", evenNumList[-1])
print("The Largest odd number in the given list",
      given_list, "=", oddNumList[-1])

Output:

Enter the total number of elements of the given list = 5
Enter some random element(integer) = 234
Enter some random element(integer) = 122
Enter some random element(integer) = 65
Enter some random element(integer) = 19
Enter some random element(integer) = 102
The Largest even number in the given list [234, 122, 65, 19, 102] = 234
The Largest odd number in the given list [234, 122, 65, 19, 102] = 65

Related Programs:

Leave a Comment