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

Groups of five consecutive numbers

Python Practice › List Programs › Program 46

Groups of five consecutive numbers

EasyList Programs · 46 of 100

Write a Python program to create lists of five consecutive numbers from 1 to 20.

Output
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]

Approach

  1. Slice range(1, 21) in steps of 5.

Solution

Python
numbers = list(range(1, 21))print([numbers[i:i + 5] for i in range(0, len(numbers), 5)])
Output
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]

Learn the concept

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

More list programs

← All 100 List Programs

Numbers and their squares as sublists

Python Practice › List Programs › Program 45

Numbers and their squares as sublists

EasyList Programs · 45 of 100

Write a Python program to create sublists of each number from 1 to 10 and its square.

Output
[[1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64], [9, 81], [10, 100]]

Approach

  1. A comprehension building [n, n * n].

Solution

Python
print([[n, n * n] for n in range(1, 11)])
Output
[[1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64], [9, 81], [10, 100]]

Learn the concept

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

More list programs

← All 100 List Programs

Print each element separately

Python Practice › List Programs › Program 44

Print each element separately

EasyList Programs · 44 of 100

Write a Python program to print each element of a list of tuples separately.

Input
[("Black", "Yellow", "Blue"), (50, 55, 60), (30.0, 50.5, 55.66)]
Output
('Black', 'Yellow', 'Blue')
(50, 55, 60)
(30.0, 50.5, 55.66)

Approach

  1. Unpack the list into print() with *.

Solution

Python
data = [("Black", "Yellow", "Blue"), (50, 55, 60), (30.0, 50.5, 55.66)]print(*data, sep="\n")
Output
('Black', 'Yellow', 'Blue')
(50, 55, 60)
(30.0, 50.5, 55.66)

Learn the concept

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

More list programs

← All 100 List Programs

Join words without spaces

Python Practice › List Programs › Program 43

Join words without spaces

EasyList Programs · 43 of 100

Write a Python program to join a list of words into one string.

Input
["Sqa", "Tools", "Best", "Learning", "Platform"]
Output
SqaToolsBestLearningPlatform

Approach

  1. "".join(words).

Solution

Python
print("".join(["Sqa", "Tools", "Best", "Learning", "Platform"]))
Output
SqaToolsBestLearningPlatform

Learn the concept

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

More list programs

← All 100 List Programs

Join numbers into one number

Python Practice › List Programs › Program 42

Join numbers into one number

EasyList Programs · 42 of 100

Write a Python program to convert a list of numbers into a single number.

Input
[12, 45, 56]
Output
124556

Approach

  1. Join their string forms and convert back to int.

Solution

Python
numbers = [12, 45, 56]print(int("".join(map(str, numbers))))
Output
124556

Learn the concept

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

More list programs

← All 100 List Programs