Is a list sorted?

Python Practice › List Programs › Program 69

Is a list sorted?

EasyList Programs · 69 of 100

Write a Python program to check whether a list is sorted in ascending order.

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

Approach

  1. Every item must be less than or equal to the next one.

Solution

Python
def is_sorted(items):    return all(a <= b for a, b in zip(items, items[1:])) print(is_sorted([1, 2, 3, 5, 7, 8, 9]), is_sorted([3, 5, 1, 6, 8, 2, 4]))
Output
True False

Line by line

Line 2

Checking neighbours is faster than comparing with sorted(items).

Learn the concept

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

More list programs

← All 100 List Programs

Elements at several indexes

Python Practice › List Programs › Program 68

Elements at several indexes

EasyList Programs · 68 of 100

Write a Python program to get the elements at a list of indexes.

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

Approach

  1. Look up each index.

Solution

Python
items = [2, 3, 4, 7, 8, 1, 5, 6, 2, 1, 8, 2]indexes = [0, 3, 5, 6]print([items[i] for i in indexes])
Output
[2, 7, 1, 5]

Learn the concept

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

More list programs

← All 100 List Programs

Count integers in a mixed list

Python Practice › List Programs › Program 67

Count integers in a mixed list

EasyList Programs · 67 of 100

Write a Python program to count the integers in a mixed list.

Input
["Hello", 45, "sqa", 23, 5, "Tools", 20]
Output
4

Approach

  1. Count items where isinstance(x, int).

Solution

Python
data = ["Hello", 45, "sqa", 23, 5, "Tools", 20]print(sum(isinstance(x, int) for x in data))
Output
4

Line by line

Line 2

Each True counts as 1 when summed.

Learn the concept

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

More list programs

← All 100 List Programs

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