Common elements of two lists

Python Practice › List Programs › Program 11

Common elements of two lists

EasyList Programs · 11 of 100

Write a Python program to get the common elements of two lists, in the order of the first list.

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

Approach

  1. Keep items of list1 that are in list2 (convert list2 to a set for speed).

Solution

Python
list1 = [4, 5, 7, 9, 2, 1]list2 = [2, 5, 8, 3, 4, 7]lookup = set(list2)print([x for x in list1 if x in lookup])
Output
[4, 5, 7, 2]

Line by line

Line 3

Membership checks on a set are fast, even for long lists.

Learn the concept

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

More list programs

← All 100 List Programs

Odd values left, even values right

Python Practice › List Programs › Program 10

Odd values left, even values right

EasyList Programs · 10 of 100

Write a Python program to rearrange a list so all odd values come first and all even values after them, keeping their order.

Input
[5, 7, 2, 8, 11, 12, 17, 19, 22]
Output
[5, 7, 11, 17, 19, 2, 8, 12, 22]

Approach

  1. Join a list of the odd values with a list of the even values.

Solution

Python
numbers = [5, 7, 2, 8, 11, 12, 17, 19, 22]odds = [n for n in numbers if n % 2]evens = [n for n in numbers if n % 2 == 0]print(odds + evens)
Output
[5, 7, 11, 17, 19, 2, 8, 12, 22]

Line by line

Line 2

n % 2 is 1 (truthy) for odd numbers.

Learn the concept

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

More list programs

← All 100 List Programs

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

Python NumPy Programs with Solutions

Python Practice › NumPy Programs

Python NumPy Programs with Solutions

100 programs100 easy0 medium0 hard

100 NumPy programs: creating arrays and matrices, element-wise tests and comparisons, indexing, reshaping, sorting, set operations, NaN handling, statistics and rounding. The outputs come from NumPy 2.5; the in-browser Try it uses an older NumPy, so a few printouts may look slightly different.

Before you start

Read the NumPy lesson first if the topic is new to you.

All programs