Squares of even numbers

Python Practice › List Programs › Program 9

Squares of even numbers

EasyList Programs · 9 of 100

Write a Python program to print the squares of all even numbers in a list.

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

Approach

  1. Filter even numbers and square them in one comprehension.

Solution

Python
numbers = [1, 2, 3, 4, 5, 6]print([n * n for n in numbers if n % 2 == 0])
Output
[4, 16, 36]

Learn the concept

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

More list programs

← All 100 List Programs

Pairs whose sum is 10

Python Practice › List Programs › Program 8

Pairs whose sum is 10

MediumList Programs · 8 of 100

Write a Python program to print every pair of elements from a list whose sum is 10.

Input
[2, 8, 5, 3, 7, 5, 1]
Output
[(2, 8), (5, 5), (3, 7)]

Approach

  1. Check every pair (i, j) with j after i so no pair is repeated.

Solution

Python
numbers = [2, 8, 5, 3, 7, 5, 1]pairs = []for i in range(len(numbers)):    for j in range(i + 1, len(numbers)):        if numbers[i] + numbers[j] == 10:            pairs.append((numbers[i], numbers[j]))print(pairs)
Output
[(2, 8), (5, 5), (3, 7)]

Line by line

Line 4

Starting the inner loop at i + 1 avoids pairing an item with itself or counting a pair twice.

Learn the concept

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

More list programs

← All 100 List Programs

Remove duplicate elements

Python Practice › List Programs › Program 7

Remove duplicate elements

MediumList Programs · 7 of 100

Write a Python program to remove every duplicate value from a list, keeping the first occurrence of each value in its original position.

Input
[5, 7, 8, 2, 5, 0, 7, 2]
Output
[5, 7, 8, 2, 0]

Approach

  1. Create an empty list for the result.
  2. Visit each value in order and add it only if it is not already in the result.

Solution 1: for loop

Python
list1 = [5, 7, 8, 2, 5, 0, 7, 2]result = [] for value in list1:    if value not in result:        result.append(value) print(result)
Output
[5, 7, 8, 2, 0]

Line by line

Line 1

The input list; 5, 7 and 2 each appear twice.

Line 2

result starts empty and collects each value once.

Line 4

The loop reads the values left to right, so the first occurrence is always seen first.

Line 5

value not in result is True only the first time a value appears.

Line 6

append() adds that first occurrence to the end of result.

Solution 2: dict.fromkeys() (shortest)

Python
list1 = [5, 7, 8, 2, 5, 0, 7, 2]result = list(dict.fromkeys(list1))print(result)
Output
[5, 7, 8, 2, 0]

Line by line

Line 2

Dictionary keys are unique and remember insertion order, so this drops repeats and keeps the first position of each value.

Watch out

list(set(list1)) also removes duplicates, but a set does not keep the original order, so it is not a correct answer when order matters.

The loop is easy to follow but checks in result for every item (O(n²)); dict.fromkeys() runs in O(n) and is what you should use in real code.

Learn the concept

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

More list programs

← All 100 List Programs

Separate even and odd numbers

Python Practice › List Programs › Program 6

Separate even and odd numbers

EasyList Programs · 6 of 100

Write a Python program to separate the even and odd numbers of a list.

Input
[5, 8, 3, 12, 7, 10]
Output
Even: [8, 12, 10]
Odd: [5, 3, 7]

Approach

  1. Two comprehensions with opposite conditions.

Solution

Python
numbers = [5, 8, 3, 12, 7, 10]evens = [n for n in numbers if n % 2 == 0]odds = [n for n in numbers if n % 2 != 0]print("Even:", evens)print("Odd:", odds)
Output
Even: [8, 12, 10]
Odd: [5, 3, 7]

Learn the concept

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

More list programs

← All 100 List Programs

Minimum and maximum

Python Practice › List Programs › Program 5

Minimum and maximum

EasyList Programs · 5 of 100

Write a Python program to find the smallest and largest elements of a list.

Input
[45, 12, 89, 3, 67]
Output
Min: 3 Max: 89
3 89

Approach

  1. Track both in one loop, or use min() and max().

Solution

Python
numbers = [45, 12, 89, 3, 67]smallest = largest = numbers[0]for n in numbers:    if n < smallest:        smallest = n    if n > largest:        largest = nprint("Min:", smallest, "Max:", largest)print(min(numbers), max(numbers))
Output
Min: 3 Max: 89
3 89

Line by line

Line 2

Both start at the first item, which works for negative numbers too.

Learn the concept

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

More list programs

← All 100 List Programs

Product of all elements

Python Practice › List Programs › Program 4

Product of all elements

EasyList Programs · 4 of 100

Write a Python program to find the product of all elements in a list.

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

Approach

  1. Start at 1 and multiply, or use math.prod().

Solution

Python
import math numbers = [2, 3, 4, 5]product = 1for n in numbers:    product *= nprint(product, math.prod(numbers))
Output
120 120

Line by line

Line 4

Starting at 1 (not 0) keeps the product correct.

Learn the concept

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

More list programs

← All 100 List Programs

Sum of all elements

Python Practice › List Programs › Program 3

Sum of all elements

EasyList Programs · 3 of 100

Write a Python program to calculate the sum of all elements in a list.

Input
[12, 7, 33, 5, 21]
Output
78 78

Approach

  1. Add each item to a total, or use sum().

Solution

Python
numbers = [12, 7, 33, 5, 21]total = 0for n in numbers:    total += nprint(total, sum(numbers))
Output
78 78

Line by line

Lines 3-4

The loop adds each number to the running total.

Line 5

The built-in sum() gives the same answer.

Learn the concept

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

More list programs

← All 100 List Programs

Combine two lists

Python Practice › List Programs › Program 2

Combine two lists

EasyList Programs · 2 of 100

Write a Python program to combine two lists.

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

Approach

  1. + creates a new list; extend() adds to an existing one.

Solution

Python
list1 = [1, 2, 3]list2 = [4, 5, 6]print(list1 + list2)list1.extend(list2)print(list1)
Output
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]

Line by line

Line 3

+ returns a new list and leaves both originals unchanged.

Line 4

extend changes list1 in place.

Learn the concept

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

More list programs

← All 100 List Programs

Square of each number in a list

Python Practice › List Programs › Program 1

Square of each number in a list

EasyList Programs · 1 of 100

Write a Python program to calculate the square of each number in a list.

Input
[2, 4, 6, 8, 10]
Output
[4, 16, 36, 64, 100]

Approach

  1. Loop over the list and append each number squared, or use a list comprehension.

Solution 1: loop

Python
numbers = [2, 4, 6, 8, 10]squares = []for n in numbers:    squares.append(n ** 2)print(squares)
Output
[4, 16, 36, 64, 100]

Line by line

Line 2

Start with an empty list for the results.

Lines 3-4

n ** 2 squares each number and append adds it to the end.

Solution 2: list comprehension

Python
numbers = [2, 4, 6, 8, 10]print([n ** 2 for n in numbers])
Output
[4, 16, 36, 64, 100]

Line by line

Line 2

The comprehension does the same loop in one expression.

Learn the concept

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

More list programs

← All 100 List Programs

Python List Programs with Solutions

Python Practice › List Programs

Python List Programs with Solutions

100 programs93 easy7 medium0 hard

100 list programs, from sums and reversing to sublists, de-duplication and run-length decoding. Lists are Python’s most used data structure, and these exercises appear in most Python interviews. Every solution is explained and shows its real output.

Before you start

Read the Lists and List Comprehension lesson first if the topic is new to you.

All programs