Average of a list

Python Practice › List Programs › Program 66

Average of a list

EasyList Programs · 66 of 100

Write a Python program to calculate the average of a list.

Input
[3, 5, 7, 2, 6, 12, 3]
Output
5.428571428571429 5.43

Approach

  1. sum / len.

Solution

Python
numbers = [3, 5, 7, 2, 6, 12, 3]avg = sum(numbers) / len(numbers)print(avg, round(avg, 2))
Output
5.428571428571429 5.43

Learn the concept

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

More list programs

← All 100 List Programs

Differences between neighbours

Python Practice › List Programs › Program 65

Differences between neighbours

EasyList Programs · 65 of 100

Write a Python program to find the difference between consecutive numbers.

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

Approach

  1. zip the list with itself shifted by one.

Solution

Python
numbers = [1, 1, 3, 4, 4, 5, 6, 7]print([b - a for a, b in zip(numbers, numbers[1:])])
Output
[0, 2, 1, 0, 1, 1, 1]

Line by line

Line 2

zip(numbers, numbers[1:]) gives each pair of neighbours.

Learn the concept

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

More list programs

← All 100 List Programs

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