Numbers that appear exactly once

Python Practice › List Programs › Program 36

Numbers that appear exactly once

EasyList Programs · 36 of 100

Write a Python program to get the numbers that appear exactly once in a list.

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

Approach

  1. Count with Counter and keep counts of 1.

Solution

Python
from collections import Counter numbers = [1, 2, 2, 3, 4, 4, 5]counts = Counter(numbers)print([n for n in numbers if counts[n] == 1])print(list(dict.fromkeys(numbers)))
Output
[1, 3, 5]
[1, 2, 3, 4, 5]

Line by line

Line 5

Values that occur only once.

Line 6

Every distinct value (duplicates removed), for comparison.

Learn the concept

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

More list programs

← All 100 List Programs

Keys and values from a list of dicts

Python Practice › List Programs › Program 35

Keys and values from a list of dicts

EasyList Programs · 35 of 100

Write a Python program to get all keys and all values from a list of single-item dictionaries.

Input
[{"a": 12}, {"b": 34}, {"c": 23}, {"d": 11}, {"e": 15}]
Output
['a', 'b', 'c', 'd', 'e']
[12, 34, 23, 11, 15]

Approach

  1. Loop over each dict’s items.

Solution

Python
data = [{"a": 12}, {"b": 34}, {"c": 23}, {"d": 11}, {"e": 15}]keys = [k for d in data for k in d]values = [v for d in data for v in d.values()]print(keys)print(values)
Output
['a', 'b', 'c', 'd', 'e']
[12, 34, 23, 11, 15]

Learn the concept

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

More list programs

← All 100 List Programs

Pair up two lists as sublists

Python Practice › List Programs › Program 34

Pair up two lists as sublists

EasyList Programs · 34 of 100

Write a Python program to combine items at the same position of two lists into sublists.

Input
[3, 5, 7, 8, 9], [1, 4, 3, 6, 2]
Output
[[3, 1], [5, 4], [7, 3], [8, 6], [9, 2]]

Approach

  1. zip pairs items; convert each pair to a list.

Solution

Python
a = [3, 5, 7, 8, 9]b = [1, 4, 3, 6, 2]print([[x, y] for x, y in zip(a, b)])
Output
[[3, 1], [5, 4], [7, 3], [8, 6], [9, 2]]

Learn the concept

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

More list programs

← All 100 List Programs

Reverse each string in a list

Python Practice › List Programs › Program 33

Reverse each string in a list

EasyList Programs · 33 of 100

Write a Python program to reverse each string in a list.

Input
["Sqa", "Tools", "Online", "Learning", "Platform"]
Output
['aqS', 'slooT', 'enilnO', 'gninraeL', 'mroftalP']

Approach

  1. Apply [::-1] to every item.

Solution

Python
words = ["Sqa", "Tools", "Online", "Learning", "Platform"]print([w[::-1] for w in words])
Output
['aqS', 'slooT', 'enilnO', 'gninraeL', 'mroftalP']

Learn the concept

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

More list programs

← All 100 List Programs

Difference between two lists

Python Practice › List Programs › Program 32

Difference between two lists

EasyList Programs · 32 of 100

Write a Python program to get the items in the first list that are not in the second.

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

Approach

  1. Keep items of list1 not in list2.

Solution

Python
a = [1, 2, 3, 4, 5]b = [2, 4, 6]print([x for x in a if x not in b])print(sorted(set(a) ^ set(b)))
Output
[1, 3, 5]
[1, 3, 5, 6]

Line by line

Line 3

Items only in a, in their original order.

Line 4

The symmetric difference gives items in exactly one of the lists.

Learn the concept

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

More list programs

← All 100 List Programs

Join elements with a special character

Python Practice › List Programs › Program 31

Join elements with a special character

EasyList Programs · 31 of 100

Write a Python program to merge all elements of a list into one string using a special character.

Input
["sqa", "tools", 2026, "python"], separator "#"
Output
sqa#tools#2026#python

Approach

  1. Convert each item to str and join with the separator.

Solution

Python
items = ["sqa", "tools", 2026, "python"]print("#".join(str(x) for x in items))
Output
sqa#tools#2026#python

Line by line

Line 2

join only accepts strings, so the number is converted first.

Learn the concept

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

More list programs

← All 100 List Programs

Second smallest number

Python Practice › List Programs › Program 30

Second smallest number

EasyList Programs · 30 of 100

Write a Python program to find the second smallest number in a list.

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

Approach

  1. Sort the unique values and take index 1.

Solution

Python
numbers = [10, 4, 32, 4, 18]print(sorted(set(numbers))[1])
Output
10

Learn the concept

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

More list programs

← All 100 List Programs

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