Second largest number

Python Practice › List Programs › Program 29

Second largest number

EasyList Programs · 29 of 100

Write a Python program to find the second largest number in a list (ignoring duplicates).

Input
[10, 45, 32, 45, 18]
Output
32

Approach

  1. Remove duplicates, sort, take the second from the end.

Solution 1: sorting

Python
numbers = [10, 45, 32, 45, 18]unique = sorted(set(numbers))print(unique[-2])
Output
32

Line by line

Line 2

set removes the duplicate 45, otherwise the answer would wrongly be 45.

Solution 2: one pass

Python
numbers = [10, 45, 32, 45, 18]first = second = float("-inf")for n in numbers:    if n > first:        first, second = n, first    elif first > n > second:        second = nprint(second)
Output
32

Line by line

Line 2

Both start below any possible number.

Lines 4-5

A new maximum pushes the old one into second place.

Lines 6-7

Numbers between the two update only the second.

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Sublists with 5 or more elements

Python Practice › List Programs › Program 28

Sublists with 5 or more elements

MediumList Programs · 28 of 100

Write a Python program to generate all consecutive sublists with 5 or more elements.

Input
[1, 2, 3, 4, 5, 6]
Output
[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6]]

Approach

  1. Two loops pick start and end positions that are at least 5 apart.

Solution

Python
items = [1, 2, 3, 4, 5, 6]subs = [items[i:j] for i in range(len(items)) for j in range(i + 5, len(items) + 1)]print(subs)
Output
[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6]]

Line by line

Line 2

j starts at i + 5, so every slice has at least 5 items.

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Does a list contain a sublist?

Python Practice › List Programs › Program 27

Does a list contain a sublist?

MediumList Programs · 27 of 100

Write a Python program to check whether a list contains another list as a consecutive run.

Input
[1, 2, 3, 4, 5] contains [3, 4]? contains [3, 5]?
Output
True False

Approach

  1. Compare each slice of the right length with the sublist.

Solution

Python
def contains(big, small):    n = len(small)    return any(big[i:i + n] == small for i in range(len(big) - n + 1)) print(contains([1, 2, 3, 4, 5], [3, 4]), contains([1, 2, 3, 4, 5], [3, 5]))
Output
True False

Line by line

Line 3

Every starting position is tried; any() stops at the first match.

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

max, min and sum

Python Practice › List Programs › Program 26

max, min and sum

EasyList Programs · 26 of 100

Write a Python program to get the maximum, minimum and sum of a list with built-in functions.

Input
[8, 3, 15, 6]
Output
15 3 32

Approach

  1. max(), min(), sum().

Solution

Python
numbers = [8, 3, 15, 6]print(max(numbers), min(numbers), sum(numbers))
Output
15 3 32

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Remove by index with pop()

Python Practice › List Programs › Program 25

Remove by index with pop()

EasyList Programs · 25 of 100

Write a Python program to remove an element at a given index with pop().

Input
["a", "b", "c", "d"], index 2
Output
c ['a', 'b', 'd']

Approach

  1. pop(index) removes and returns the item.

Solution

Python
items = ["a", "b", "c", "d"]removed = items.pop(2)print(removed, items)
Output
c ['a', 'b', 'd']

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

sort() vs sorted()

Python Practice › List Programs › Program 24

sort() vs sorted()

EasyList Programs · 24 of 100

Write a Python program to sort a list with sort() and with sorted().

Input
[5, 2, 9, 1, 7]
Output
[1, 2, 5, 7, 9] [5, 2, 9, 1, 7]
[9, 7, 5, 2, 1]

Approach

  1. sorted() returns a new list; sort() sorts in place.

Solution

Python
numbers = [5, 2, 9, 1, 7]print(sorted(numbers), numbers)numbers.sort(reverse=True)print(numbers)
Output
[1, 2, 5, 7, 9] [5, 2, 9, 1, 7]
[9, 7, 5, 2, 1]

Line by line

Line 2

The original order is unchanged by sorted().

Line 3

sort() changes the list; reverse=True gives descending order.

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Add two lists with extend()

Python Practice › List Programs › Program 23

Add two lists with extend()

EasyList Programs · 23 of 100

Write a Python program to add one list to another with extend().

Input
[1, 2], [3, 4]
Output
[1, 2, 3, 4]

Approach

  1. a.extend(b) adds every item of b to a.

Solution

Python
a = [1, 2]b = [3, 4]a.extend(b)print(a)
Output
[1, 2, 3, 4]

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Words that contain vowels

Python Practice › List Programs › Program 22

Words that contain vowels

EasyList Programs · 22 of 100

Write a Python program to get the words of a string that contain at least one vowel.

Input
"www Student ppp are qqqq learning Python vvv"
Output
['Student', 'are', 'learning', 'Python']

Approach

  1. Keep words where any character is a vowel.

Solution

Python
text = "www Student ppp are qqqq learning Python vvv"print([w for w in text.split() if any(ch in "aeiouAEIOU" for ch in w)])
Output
['Student', 'are', 'learning', 'Python']

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Is a list a palindrome?

Python Practice › List Programs › Program 21

Is a list a palindrome?

EasyList Programs · 21 of 100

Write a Python program to check whether a list reads the same forwards and backwards.

Input
[1, 2, 3, 2, 1], [1, 2, 3]
Output
[1, 2, 3, 2, 1] True
[1, 2, 3] False

Approach

  1. Compare the list with its reverse.

Solution

Python
for items in [[1, 2, 3, 2, 1], [1, 2, 3]]:    print(items, items == items[::-1])
Output
[1, 2, 3, 2, 1] True
[1, 2, 3] False

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Elements divisible by 3 and 7

Python Practice › List Programs › Program 20

Elements divisible by 3 and 7

EasyList Programs · 20 of 100

Write a Python program to get all elements divisible by both 3 and 7.

Input
[21, 14, 63, 9, 42, 50]
Output
[21, 63, 42]

Approach

  1. Divisible by both 3 and 7 means divisible by 21.

Solution

Python
numbers = [21, 14, 63, 9, 42, 50]print([n for n in numbers if n % 3 == 0 and n % 7 == 0])
Output
[21, 63, 42]

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs