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

Positives left, negatives right

Python Practice › List Programs › Program 49

Positives left, negatives right

EasyList Programs · 49 of 100

Write a Python program to move all positive numbers to the left and negative numbers to the right, keeping their order.

Input
[2, -4, 6, 44, -7, 8, -1, -10]
Output
[2, 6, 44, 8, -4, -7, -1, -10]

Approach

  1. Join a list of non-negative values with a list of negative values.

Solution

Python
numbers = [2, -4, 6, 44, -7, 8, -1, -10]print([n for n in numbers if n >= 0] + [n for n in numbers if n < 0])
Output
[2, 6, 44, 8, -4, -7, -1, -10]

Learn the concept

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

More list programs

← All 100 List Programs

Iterate two lists at once

Python Practice › List Programs › Program 48

Iterate two lists at once

EasyList Programs · 48 of 100

Write a Python program to iterate over two lists simultaneously and build a list of pairs.

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

Approach

  1. zip both lists.

Solution

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

Learn the concept

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

More list programs

← All 100 List Programs

Add a prefix to every item

Python Practice › List Programs › Program 47

Add a prefix to every item

EasyList Programs · 47 of 100

Write a Python program to insert a string at the beginning of every item.

Input
[1, 2, 3, 4, 5], "Sqa"
Output
['Sqa1', 'Sqa2', 'Sqa3', 'Sqa4', 'Sqa5']

Approach

  1. An f-string for each item.

Solution

Python
print([f"Sqa{n}" for n in [1, 2, 3, 4, 5]])
Output
['Sqa1', 'Sqa2', 'Sqa3', 'Sqa4', 'Sqa5']

Learn the concept

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

More list programs

← All 100 List Programs