# Python Exercises: Find the largest odd number in a list of integers

## Python List: Exercise - 276 with Solution

Write a Python program to find the largest odd number in a given list of integers.

**Sample Data:**

([0, 9, 2, 4, 5, 6]) -> 9

([-4, 0, 6, 1, 0, 2]) -> 1

([1, 2, 3]) -> 3

([-4, 0, 5, 1, 0, 1]) -> 5

**Sample Solution-1:**

**Python Code:**

```
def test(nums):
odd_nums = [x for x in nums if x%2!=0]
return -1 if len(odd_nums) == 0 else max(odd_nums)
nums = [0, 9, 2, 4, 5, 6]
print("Original list:")
print(nums)
print("Find the largest odd number in the said list:", test(nums))
nums = [-4, 0, 6, 1, 0, 2]
print("\nOriginal list:")
print(nums)
print("Find the largest odd number in the said list:", test(nums))
nums = [1, 2, 3]
print("\nOriginal list:")
print(nums)
print("Find the largest odd number in the said list:", test(nums))
nums = [-4, 0, 5, 1, 0, 1]
print("\nOriginal list:")
print(nums)
print("Find the largest odd number in the said list:", test(nums))
```

Sample Output:

Original list: [0, 9, 2, 4, 5, 6] Find the largest odd number in the said list: 9 Original list: [-4, 0, 6, 1, 0, 2] Find the largest odd number in the said list: 1 Original list: [1, 2, 3] Find the largest odd number in the said list: 3 Original list: [-4, 0, 5, 1, 0, 1] Find the largest odd number in the said list: 5

**Flowchart:**

## Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

**Sample Solution-2:**

**Python Code:**

```
def test(nums):
result = -1
for x in nums:
if x % 2 and x > result:
result = x
return result
nums = [0, 9, 2, 4, 5, 6]
print("Original list:")
print(nums)
print("Find the largest odd number in the said list:", test(nums))
nums = [-4, 0, 6, 1, 0, 2]
print("\nOriginal list:")
print(nums)
print("Find the largest odd number in the said list:", test(nums))
nums = [1, 2, 3]
print("\nOriginal list:")
print(nums)
print("Find the largest odd number in the said list:", test(nums))
nums = [-4, 0, 5, 1, 0, 1]
print("\nOriginal list:")
print(nums)
print("Find the largest odd number in the said list:", test(nums))
```

Sample Output:

Original list: [0, 9, 2, 4, 5, 6] Find the largest odd number in the said list: 9 Original list: [-4, 0, 6, 1, 0, 2] Find the largest odd number in the said list: 1 Original list: [1, 2, 3] Find the largest odd number in the said list: 3 Original list: [-4, 0, 5, 1, 0, 1] Find the largest odd number in the said list: 5

**Flowchart:**

## Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

**Python Code Editor:**

**Have another way to solve this solution? Contribute your code (and comments) through Disqus.**

**Previous Python Exercise:** Sum of all list elements except current element.

**Next Python Exercise:** Largest, lowest gap between sorted values of a list.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

## Python: Tips of the Day

**Generates a list, containing the Fibonacci sequence, up until the nth term:**

Example:

def fibonacci(n): if n <= 0: return [0] sequence = [0, 1] while len(sequence) <= n: next_value = sequence[len(sequence) - 1] + sequence[len(sequence) - 2] sequence.append(next_value) return sequence print(fibonacci(7))

Output:

[0, 1, 1, 2, 3, 5, 8, 13]

**Weekly Trends**- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises