max, min and sum

Python Practice › List Programs › Program 26

max, min and sum

EasyList Programs · 26 of 100

Write a Python program to get the maximum, minimum and sum of a list with built-in functions.

Input
[8, 3, 15, 6]
Output
15 3 32

Approach

  1. max(), min(), sum().

Solution

Python
numbers = [8, 3, 15, 6]print(max(numbers), min(numbers), sum(numbers))
Output
15 3 32

Learn the concept

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

More list programs

← All 100 List Programs

Remove by index with pop()

Python Practice › List Programs › Program 25

Remove by index with pop()

EasyList Programs · 25 of 100

Write a Python program to remove an element at a given index with pop().

Input
["a", "b", "c", "d"], index 2
Output
c ['a', 'b', 'd']

Approach

  1. pop(index) removes and returns the item.

Solution

Python
items = ["a", "b", "c", "d"]removed = items.pop(2)print(removed, items)
Output
c ['a', 'b', 'd']

Learn the concept

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

More list programs

← All 100 List Programs

sort() vs sorted()

Python Practice › List Programs › Program 24

sort() vs sorted()

EasyList Programs · 24 of 100

Write a Python program to sort a list with sort() and with sorted().

Input
[5, 2, 9, 1, 7]
Output
[1, 2, 5, 7, 9] [5, 2, 9, 1, 7]
[9, 7, 5, 2, 1]

Approach

  1. sorted() returns a new list; sort() sorts in place.

Solution

Python
numbers = [5, 2, 9, 1, 7]print(sorted(numbers), numbers)numbers.sort(reverse=True)print(numbers)
Output
[1, 2, 5, 7, 9] [5, 2, 9, 1, 7]
[9, 7, 5, 2, 1]

Line by line

Line 2

The original order is unchanged by sorted().

Line 3

sort() changes the list; reverse=True gives descending order.

Learn the concept

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

More list programs

← All 100 List Programs

Add two lists with extend()

Python Practice › List Programs › Program 23

Add two lists with extend()

EasyList Programs · 23 of 100

Write a Python program to add one list to another with extend().

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

Approach

  1. a.extend(b) adds every item of b to a.

Solution

Python
a = [1, 2]b = [3, 4]a.extend(b)print(a)
Output
[1, 2, 3, 4]

Learn the concept

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

More list programs

← All 100 List Programs

Words that contain vowels

Python Practice › List Programs › Program 22

Words that contain vowels

EasyList Programs · 22 of 100

Write a Python program to get the words of a string that contain at least one vowel.

Input
"www Student ppp are qqqq learning Python vvv"
Output
['Student', 'are', 'learning', 'Python']

Approach

  1. Keep words where any character is a vowel.

Solution

Python
text = "www Student ppp are qqqq learning Python vvv"print([w for w in text.split() if any(ch in "aeiouAEIOU" for ch in w)])
Output
['Student', 'are', 'learning', 'Python']

Learn the concept

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

More list programs

← All 100 List Programs

Is a list a palindrome?

Python Practice › List Programs › Program 21

Is a list a palindrome?

EasyList Programs · 21 of 100

Write a Python program to check whether a list reads the same forwards and backwards.

Input
[1, 2, 3, 2, 1], [1, 2, 3]
Output
[1, 2, 3, 2, 1] True
[1, 2, 3] False

Approach

  1. Compare the list with its reverse.

Solution

Python
for items in [[1, 2, 3, 2, 1], [1, 2, 3]]:    print(items, items == items[::-1])
Output
[1, 2, 3, 2, 1] True
[1, 2, 3] False

Learn the concept

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

More list programs

← All 100 List Programs

Elements divisible by 3 and 7

Python Practice › List Programs › Program 20

Elements divisible by 3 and 7

EasyList Programs · 20 of 100

Write a Python program to get all elements divisible by both 3 and 7.

Input
[21, 14, 63, 9, 42, 50]
Output
[21, 63, 42]

Approach

  1. Divisible by both 3 and 7 means divisible by 21.

Solution

Python
numbers = [21, 14, 63, 9, 42, 50]print([n for n in numbers if n % 3 == 0 and n % 7 == 0])
Output
[21, 63, 42]

Learn the concept

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

More list programs

← All 100 List Programs

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