Copy a list

Python Practice › List Programs › Program 16

Copy a list

EasyList Programs · 16 of 100

Write a Python program to copy a list so that changing the copy does not change the original.

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

Approach

  1. Use copy(), list() or slicing [:].

Solution

Python
original = [1, 2, 3]copy1 = original.copy()copy2 = original[:]copy1.append(4)print(original, copy1, copy2)
Output
[1, 2, 3] [1, 2, 3, 4] [1, 2, 3]

Line by line

Lines 2-3

Both create new lists.

Line 4

Changing the copy leaves the original alone.

Watch out

copy2 = original would not copy; both names would refer to the same list.

Learn the concept

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

More list programs

← All 100 List Programs

reversed() and reverse()

Python Practice › List Programs › Program 15

reversed() and reverse()

EasyList Programs · 15 of 100

Write a Python program to reverse a list with reversed() and with reverse().

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

Approach

  1. reversed() gives an iterator; reverse() changes the list in place.

Solution

Python
numbers = [1, 2, 3, 4, 5]print(list(reversed(numbers)))numbers.reverse()print(numbers)
Output
[5, 4, 3, 2, 1]
[5, 4, 3, 2, 1]

Line by line

Line 2

reversed() returns an iterator, so wrap it in list() to see the items.

Line 3

reverse() returns None and reverses numbers itself.

Learn the concept

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

More list programs

← All 100 List Programs

Reverse a list with slicing

Python Practice › List Programs › Program 14

Reverse a list with slicing

EasyList Programs · 14 of 100

Write a Python program to reverse a list using slicing.

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

Approach

  1. [::-1] returns a reversed copy.

Solution

Python
numbers = [1, 2, 3, 4, 5]print(numbers[::-1], numbers)
Output
[5, 4, 3, 2, 1] [1, 2, 3, 4, 5]

Line by line

Line 2

The slice creates a new list; the original is unchanged.

Learn the concept

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

More list programs

← All 100 List Programs

Reverse a list with a while loop

Python Practice › List Programs › Program 13

Reverse a list with a while loop

EasyList Programs · 13 of 100

Write a Python program to reverse a list using a while loop.

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

Approach

  1. Walk an index from the last position down to 0.

Solution

Python
numbers = [1, 2, 3, 4, 5]result = []i = len(numbers) - 1while i >= 0:    result.append(numbers[i])    i -= 1print(result)
Output
[5, 4, 3, 2, 1]

Learn the concept

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

More list programs

← All 100 List Programs

Reverse a list with a for loop

Python Practice › List Programs › Program 12

Reverse a list with a for loop

EasyList Programs · 12 of 100

Write a Python program to reverse a list using a for loop.

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

Approach

  1. Insert each item at the front of a new list.

Solution

Python
numbers = [1, 2, 3, 4, 5]reversed_list = []for n in numbers:    reversed_list.insert(0, n)print(reversed_list)
Output
[5, 4, 3, 2, 1]

Line by line

Line 4

Inserting at index 0 puts each new item before the earlier ones.

Learn the concept

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

More list programs

← All 100 List Programs

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