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

At least one common member

Python Practice › List Programs › Program 41

At least one common member

EasyList Programs · 41 of 100

Write a Python program that returns True if two lists have at least one common member.

Input
["a", "b"] and ["c", "b"]
Output
True

Approach

  1. any(x in b for x in a).

Solution

Python
a, b = ["a", "b"], ["c", "b"]print(any(x in b for x in a))
Output
True

Learn the concept

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

More list programs

← All 100 List Programs

Remove elements at odd indexes

Python Practice › List Programs › Program 40

Remove elements at odd indexes

EasyList Programs · 40 of 100

Write a Python program to remove all elements at odd indexes.

Input
[12, 32, 33, 5, 4, 7, 33]
Output
[12, 33, 4, 33]

Approach

  1. Keep every second item starting at index 0.

Solution

Python
items = [12, 32, 33, 5, 4, 7, 33]print(items[::2])
Output
[12, 33, 4, 33]

Learn the concept

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

More list programs

← All 100 List Programs

Check if an element exists

Python Practice › List Programs › Program 39

Check if an element exists

EasyList Programs · 39 of 100

Write a Python program to check whether an element exists in a list.

Input
[3, 9, 27], check 9 and 4
Output
9 exists
4 does not exist

Approach

  1. Use in.

Solution

Python
numbers = [3, 9, 27]for x in [9, 4]:    print(x, "exists" if x in numbers else "does not exist")
Output
9 exists
4 does not exist

Learn the concept

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

More list programs

← All 100 List Programs

Replace the first and last items

Python Practice › List Programs › Program 38

Replace the first and last items

EasyList Programs · 38 of 100

Write a Python program to replace the first and last numbers of a list with “SQA” and “Tools”.

Input
[12, 32, 33, 5, 4, 7]
Output
['SQA', 32, 33, 5, 4, 'Tools']

Approach

  1. Assign to index 0 and index -1.

Solution

Python
numbers = [12, 32, 33, 5, 4, 7]numbers[0] = "SQA"numbers[-1] = "Tools"print(numbers)
Output
['SQA', 32, 33, 5, 4, 'Tools']

Learn the concept

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

More list programs

← All 100 List Programs

Convert a string to a list

Python Practice › List Programs › Program 37

Convert a string to a list

EasyList Programs · 37 of 100

Write a Python program to convert a string into a list of words and a list of characters.

Input
"learn python"
Output
['learn', 'python']
['l', 'e', 'a', 'r', 'n', ' ', 'p', 'y', 't', 'h', 'o', 'n']

Approach

  1. split() for words, list() for characters.

Solution

Python
text = "learn python"print(text.split())print(list(text))
Output
['learn', 'python']
['l', 'e', 'a', 'r', 'n', ' ', 'p', 'y', 't', 'h', 'o', 'n']

Learn the concept

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

More list programs

← All 100 List Programs