Strings of a given length

Python Practice › List Programs › Program 64

Strings of a given length

EasyList Programs · 64 of 100

Write a Python program to extract the strings of a given length.

Input
["Python", "Sqatools", "Practice", "Program", "test", "list"], size 8
Output
['Sqatools', 'Practice']

Approach

  1. Filter by len().

Solution

Python
words = ["Python", "Sqatools", "Practice", "Program", "test", "list"]print([w for w in words if len(w) == 8])
Output
['Sqatools', 'Practice']

Learn the concept

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

More list programs

← All 100 List Programs

Sort sublists by their sum

Python Practice › List Programs › Program 63

Sort sublists by their sum

EasyList Programs · 63 of 100

Write a Python program to sort a list of sublists by the sum of each sublist.

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

Approach

  1. sorted with key=sum.

Solution

Python
data = [[3, 5, 6], [2, 1, 3], [5, 1, 1], [1, 2, 1], [0, 4, 1]]print(sorted(data, key=sum))
Output
[[1, 2, 1], [0, 4, 1], [2, 1, 3], [5, 1, 1], [3, 5, 6]]

Learn the concept

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

More list programs

← All 100 List Programs

Max and min of a mixed list

Python Practice › List Programs › Program 62

Max and min of a mixed list

EasyList Programs · 62 of 100

Write a Python program to find the maximum and minimum numbers in a list that also contains strings.

Input
["Sqa", 6, 5, 2, "Tools"]
Output
[6, 2]

Approach

  1. Keep only numbers, then use max and min.

Solution

Python
data = ["Sqa", 6, 5, 2, "Tools"]nums = [x for x in data if isinstance(x, (int, float))]print([max(nums), min(nums)])
Output
[6, 2]

Line by line

Line 2

Comparing a string with a number raises TypeError, so the strings are filtered out first.

Learn the concept

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

More list programs

← All 100 List Programs

Capitalise the first and last letters

Python Practice › List Programs › Program 61

Capitalise the first and last letters

EasyList Programs · 61 of 100

Write a Python program to make the first and last letters of each word uppercase, and also print every word in lowercase.

Input
["Learn", "python", "From", "Sqa", "tools"]
Output
['LearN', 'PythoN', 'FroM', 'SqA', 'ToolS']
['learn', 'python', 'from', 'sqa', 'tools']

Approach

  1. Upper-case the ends and keep the middle.

Solution

Python
words = ["Learn", "python", "From", "Sqa", "tools"]print([w[0].upper() + w[1:-1] + w[-1].upper() for w in words])print([w.lower() for w in words])
Output
['LearN', 'PythoN', 'FroM', 'SqA', 'ToolS']
['learn', 'python', 'from', 'sqa', 'tools']

Learn the concept

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

More list programs

← All 100 List Programs

Zip two lists of lists

Python Practice › List Programs › Program 60

Zip two lists of lists

EasyList Programs · 60 of 100

Write a Python program to join two lists of lists item by item.

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

Approach

  1. zip the outer lists and concatenate each pair.

Solution

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

Learn the concept

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

More list programs

← All 100 List Programs

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