Remove negative values

Python Practice › List Programs › Program 19

Remove negative values

EasyList Programs · 19 of 100

Write a Python program to remove negative values from a list.

Input
[4, -2, 0, 7, -9, 3]
Output
[4, 0, 7, 3]

Approach

  1. Keep values that are not below 0.

Solution

Python
numbers = [4, -2, 0, 7, -9, 3]print([n for n in numbers if n >= 0])
Output
[4, 0, 7, 3]

Learn the concept

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

More list programs

← All 100 List Programs

Remove the 1st, 3rd and 6th elements

Python Practice › List Programs › Program 18

Remove the 1st, 3rd and 6th elements

EasyList Programs · 18 of 100

Write a Python program to remove the 1st, 3rd and 6th elements of a list.

Input
["a", "b", "c", "d", "e", "f", "g"]
Output
['b', 'd', 'e', 'g']

Approach

  1. Keep items whose index is not 0, 2 or 5.

Solution

Python
items = ["a", "b", "c", "d", "e", "f", "g"]remove = {0, 2, 5}print([x for i, x in enumerate(items) if i not in remove])
Output
['b', 'd', 'e', 'g']

Line by line

Line 2

Positions 1, 3 and 6 are indexes 0, 2 and 5.

Note

Deleting with del one by one shifts later indexes; building a new list avoids that trap.

Learn the concept

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

More list programs

← All 100 List Programs

Do two lists share a member?

Python Practice › List Programs › Program 17

Do two lists share a member?

EasyList Programs · 17 of 100

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

Input
[1, 2, 3] and [5, 3, 9]; [1, 2] and [7, 8]
Output
True False

Approach

  1. Two sets are disjoint when they share nothing.

Solution

Python
def has_common(a, b):    return not set(a).isdisjoint(b) print(has_common([1, 2, 3], [5, 3, 9]), has_common([1, 2], [7, 8]))
Output
True False

Learn the concept

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

More list programs

← All 100 List Programs

Copy a list

Python Practice › List Programs › Program 16

Copy a list

EasyList Programs · 16 of 100

Write a Python program to copy a list so that changing the copy does not change the original.

Input
[1, 2, 3]
Output
[1, 2, 3] [1, 2, 3, 4] [1, 2, 3]

Approach

  1. Use copy(), list() or slicing [:].

Solution

Python
original = [1, 2, 3]copy1 = original.copy()copy2 = original[:]copy1.append(4)print(original, copy1, copy2)
Output
[1, 2, 3] [1, 2, 3, 4] [1, 2, 3]

Line by line

Lines 2-3

Both create new lists.

Line 4

Changing the copy leaves the original alone.

Watch out

copy2 = original would not copy; both names would refer to the same list.

Learn the concept

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

More list programs

← All 100 List Programs

reversed() and reverse()

Python Practice › List Programs › Program 15

reversed() and reverse()

EasyList Programs · 15 of 100

Write a Python program to reverse a list with reversed() and with reverse().

Input
[1, 2, 3, 4, 5]
Output
[5, 4, 3, 2, 1]
[5, 4, 3, 2, 1]

Approach

  1. reversed() gives an iterator; reverse() changes the list in place.

Solution

Python
numbers = [1, 2, 3, 4, 5]print(list(reversed(numbers)))numbers.reverse()print(numbers)
Output
[5, 4, 3, 2, 1]
[5, 4, 3, 2, 1]

Line by line

Line 2

reversed() returns an iterator, so wrap it in list() to see the items.

Line 3

reverse() returns None and reverses numbers itself.

Learn the concept

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

More list programs

← All 100 List Programs

Reverse a list with slicing

Python Practice › List Programs › Program 14

Reverse a list with slicing

EasyList Programs · 14 of 100

Write a Python program to reverse a list using slicing.

Input
[1, 2, 3, 4, 5]
Output
[5, 4, 3, 2, 1] [1, 2, 3, 4, 5]

Approach

  1. [::-1] returns a reversed copy.

Solution

Python
numbers = [1, 2, 3, 4, 5]print(numbers[::-1], numbers)
Output
[5, 4, 3, 2, 1] [1, 2, 3, 4, 5]

Line by line

Line 2

The slice creates a new list; the original is unchanged.

Learn the concept

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

More list programs

← All 100 List Programs

Reverse a list with a while loop

Python Practice › List Programs › Program 13

Reverse a list with a while loop

EasyList Programs · 13 of 100

Write a Python program to reverse a list using a while loop.

Input
[1, 2, 3, 4, 5]
Output
[5, 4, 3, 2, 1]

Approach

  1. Walk an index from the last position down to 0.

Solution

Python
numbers = [1, 2, 3, 4, 5]result = []i = len(numbers) - 1while i >= 0:    result.append(numbers[i])    i -= 1print(result)
Output
[5, 4, 3, 2, 1]

Learn the concept

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

More list programs

← All 100 List Programs

Reverse a list with a for loop

Python Practice › List Programs › Program 12

Reverse a list with a for loop

EasyList Programs · 12 of 100

Write a Python program to reverse a list using a for loop.

Input
[1, 2, 3, 4, 5]
Output
[5, 4, 3, 2, 1]

Approach

  1. Insert each item at the front of a new list.

Solution

Python
numbers = [1, 2, 3, 4, 5]reversed_list = []for n in numbers:    reversed_list.insert(0, n)print(reversed_list)
Output
[5, 4, 3, 2, 1]

Line by line

Line 4

Inserting at index 0 puts each new item before the earlier ones.

Learn the concept

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

More list programs

← All 100 List Programs

Common elements of two lists

Python Practice › List Programs › Program 11

Common elements of two lists

EasyList Programs · 11 of 100

Write a Python program to get the common elements of two lists, in the order of the first list.

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

Approach

  1. Keep items of list1 that are in list2 (convert list2 to a set for speed).

Solution

Python
list1 = [4, 5, 7, 9, 2, 1]list2 = [2, 5, 8, 3, 4, 7]lookup = set(list2)print([x for x in list1 if x in lookup])
Output
[4, 5, 7, 2]

Line by line

Line 3

Membership checks on a set are fast, even for long lists.

Learn the concept

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

More list programs

← All 100 List Programs

Odd values left, even values right

Python Practice › List Programs › Program 10

Odd values left, even values right

EasyList Programs · 10 of 100

Write a Python program to rearrange a list so all odd values come first and all even values after them, keeping their order.

Input
[5, 7, 2, 8, 11, 12, 17, 19, 22]
Output
[5, 7, 11, 17, 19, 2, 8, 12, 22]

Approach

  1. Join a list of the odd values with a list of the even values.

Solution

Python
numbers = [5, 7, 2, 8, 11, 12, 17, 19, 22]odds = [n for n in numbers if n % 2]evens = [n for n in numbers if n % 2 == 0]print(odds + evens)
Output
[5, 7, 11, 17, 19, 2, 8, 12, 22]

Line by line

Line 2

n % 2 is 1 (truthy) for odd numbers.

Learn the concept

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

More list programs

← All 100 List Programs