3 × 3 grid

Python Practice › List Programs › Program 59

3 × 3 grid

EasyList Programs · 59 of 100

Write a Python program to create a 3 × 3 grid where every row is [4, 5, 6].

Output
[[99, 5, 6], [4, 5, 6], [4, 5, 6]]

Approach

  1. Build each row with a comprehension so the rows are separate lists.

Solution

Python
grid = [[4, 5, 6] for _ in range(3)]grid[0][0] = 99print(grid)
Output
[[99, 5, 6], [4, 5, 6], [4, 5, 6]]

Line by line

Line 2

Changing one row proves the rows are independent.

Watch out

[[4, 5, 6]] * 3 repeats the same list object three times, so changing one row would change all of them.

Learn the concept

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

More list programs

← All 100 List Programs

Pick random numbers from a list

Python Practice › List Programs › Program 58

Pick random numbers from a list

EasyList Programs · 58 of 100

Write a Python program to select 4 random numbers from a list.

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

Approach

  1. random.sample picks without repeats; seed for repeatable output.

Solution

Python
import random random.seed(5)numbers = [1, 4, 5, 7, 3, 2, 9]print(random.sample(numbers, 4))print(random.choice(numbers))
Output
[3, 5, 2, 1]
9

Learn the concept

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

More list programs

← All 100 List Programs

Insert an item at an index

Python Practice › List Programs › Program 57

Insert an item at an index

EasyList Programs · 57 of 100

Write a Python program to insert an item at a specific position.

Input
[2, 4, 6, 8, 3, 22], index 3, item 55
Output
[2, 4, 6, 55, 8, 3, 22]

Approach

  1. insert(index, item).

Solution

Python
items = [2, 4, 6, 8, 3, 22]items.insert(3, 55)print(items)
Output
[2, 4, 6, 55, 8, 3, 22]

Learn the concept

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

More list programs

← All 100 List Programs

Split a list at a given length

Python Practice › List Programs › Program 56

Split a list at a given length

EasyList Programs · 56 of 100

Write a Python program to split a list into two parts where the first part has a given length.

Input
[4, 6, 7, 3, 2, 5, 6, 7, 6, 4], length 4
Output
[[4, 6, 7, 3], [2, 5, 6, 7, 6, 4]]

Approach

  1. Two slices at the split point.

Solution

Python
items = [4, 6, 7, 3, 2, 5, 6, 7, 6, 4]n = 4print([items[:n], items[n:]])
Output
[[4, 6, 7, 3], [2, 5, 6, 7, 6, 4]]

Learn the concept

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

More list programs

← All 100 List Programs

Pack consecutive duplicates

Python Practice › List Programs › Program 55

Pack consecutive duplicates

EasyList Programs · 55 of 100

Write a Python program to pack consecutive duplicates into sublists.

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

Approach

  1. itertools.groupby groups consecutive equal items.

Solution

Python
from itertools import groupby data = [0, 0, 1, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9]print([list(group) for _, group in groupby(data)])
Output
[[0, 0], [1], [2], [3], [4, 4], [5], [6, 6], [7], [8, 8], [9]]

Learn the concept

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

More list programs

← All 100 List Programs

Remove consecutive duplicates

Python Practice › List Programs › Program 54

Remove consecutive duplicates

EasyList Programs · 54 of 100

Write a Python program to remove consecutive duplicates from a list.

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

Approach

  1. Keep an item when it differs from the previous one.

Solution

Python
data = [0, 0, 1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9, 4, 4]result = [x for i, x in enumerate(data) if i == 0 or x != data[i - 1]]print(result)
Output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4]

Line by line

Line 2

The first item is always kept; others only when they change.

Learn the concept

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

More list programs

← All 100 List Programs

Count empty dictionaries

Python Practice › List Programs › Program 53

Count empty dictionaries

EasyList Programs · 53 of 100

Write a Python program to count the empty dictionaries in a list (ignoring other empty containers).

Input
[{}, {"a": "sqatools"}, [], {}, {"a": 123}, {}, {}, ()]
Output
4

Approach

  1. Count items that are dicts and empty.

Solution

Python
data = [{}, {"a": "sqatools"}, [], {}, {"a": 123}, {}, {}, ()]print(sum(1 for x in data if isinstance(x, dict) and not x))
Output
4
Note

There are 4 empty dicts in this list; the original sample answer said 3.

Learn the concept

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

More list programs

← All 100 List Programs

Items starting with a letter

Python Practice › List Programs › Program 52

Items starting with a letter

EasyList Programs · 52 of 100

Write a Python program to find the items that start with a given character.

Input
["abbcd", "ppq", "abdd", "agr", "bhr", "sqqa", "tools", "bgr"]; letters a, b, c
Output
a ['abbcd', 'abdd', 'agr']
b ['bhr', 'bgr']
c []

Approach

  1. Filter with startswith().

Solution

Python
items = ["abbcd", "ppq", "abdd", "agr", "bhr", "sqqa", "tools", "bgr"]for letter in "abc":    print(letter, [w for w in items if w.startswith(letter)])
Output
a ['abbcd', 'abdd', 'agr']
b ['bhr', 'bgr']
c []

Learn the concept

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

More list programs

← All 100 List Programs

Sublist with the highest sum

Python Practice › List Programs › Program 51

Sublist with the highest sum

EasyList Programs · 51 of 100

Write a Python program to find the sublist whose elements add up to the highest total.

Input
[[11, 2, 3], [4, 15, 6], [10, 11, 12], [7, 8, 19]]
Output
[7, 8, 19]

Approach

  1. max with key=sum.

Solution

Python
data = [[11, 2, 3], [4, 15, 6], [10, 11, 12], [7, 8, 19]]print(max(data, key=sum))
Output
[7, 8, 19]

Line by line

Line 2

The sums are 16, 25, 33 and 34.

Learn the concept

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

More list programs

← All 100 List Programs

Move zeros to the end

Python Practice › List Programs › Program 50

Move zeros to the end

EasyList Programs · 50 of 100

Write a Python program to move all zeros to the end of a list.

Input
[3, 4, 0, 0, 0, 0, 6, 0, 4, 0, 22, 0, 0, 3, 21, 0]
Output
[3, 4, 6, 4, 22, 3, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Approach

  1. Non-zero values followed by the right number of zeros.

Solution

Python
numbers = [3, 4, 0, 0, 0, 0, 6, 0, 4, 0, 22, 0, 0, 3, 21, 0]non_zero = [n for n in numbers if n != 0]print(non_zero + [0] * (len(numbers) - len(non_zero)))
Output
[3, 4, 6, 4, 22, 3, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Line by line

Line 3

[0] * k makes a list of k zeros.

Learn the concept

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

More list programs

← All 100 List Programs