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

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