Prime numbers in a list

Python Practice › List Programs › Program 82

Prime numbers in a list

EasyList Programs · 82 of 100

Write a Python program to get the prime numbers from a list.

Input
[11, 8, 7, 19, 6, 29]
Output
[11, 7, 19, 29]

Approach

  1. Keep numbers with no divisor from 2 to √n.

Solution

Python
def is_prime(n):    return n > 1 and all(n % d for d in range(2, int(n ** 0.5) + 1)) print([n for n in [11, 8, 7, 19, 6, 29] if is_prime(n)])
Output
[11, 7, 19, 29]

Learn the concept

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

More list programs

← All 100 List Programs

Count vowels in a list of words

Python Practice › List Programs › Program 81

Count vowels in a list of words

EasyList Programs · 81 of 100

Write a Python program to count the vowels in a list of words.

Input
["Learning", "Python", "From", "SqaTool"]
Output
8

Approach

  1. Join and count characters in “aeiou”.

Solution

Python
words = ["Learning", "Python", "From", "SqaTool"]print(sum(ch in "aeiouAEIOU" for w in words for ch in w))
Output
8
Note

The words contain 3 + 1 + 1 + 3 = 8 vowels.

Learn the concept

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

More list programs

← All 100 List Programs

Palindrome numbers in a list

Python Practice › List Programs › Program 80

Palindrome numbers in a list

EasyList Programs · 80 of 100

Write a Python program to get the palindrome numbers from a list.

Input
[121, 134, 354, 383, 892, 232]
Output
[121, 383, 232]

Approach

  1. Keep numbers equal to their reverse.

Solution

Python
numbers = [121, 134, 354, 383, 892, 232]print([n for n in numbers if str(n) == str(n)[::-1]])
Output
[121, 383, 232]
Note

383 is a palindrome too; the original sample answer listed 282 instead.

Learn the concept

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

More list programs

← All 100 List Programs

Reverse every number in a list

Python Practice › List Programs › Program 79

Reverse every number in a list

EasyList Programs · 79 of 100

Write a Python program to reverse the digits of every number in a list.

Input
[123, 145, 633, 654, 254]
Output
[321, 541, 336, 456, 452]

Approach

  1. Reverse each number’s string and convert back.

Solution

Python
numbers = [123, 145, 633, 654, 254]print([int(str(n)[::-1]) for n in numbers])
Output
[321, 541, 336, 456, 452]

Learn the concept

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

More list programs

← All 100 List Programs

Fibonacci numbers from 1 to 20

Python Practice › List Programs › Program 78

Fibonacci numbers from 1 to 20

EasyList Programs · 78 of 100

Write a Python program to get the Fibonacci numbers between 1 and 20.

Output
[1, 1, 2, 3, 5, 8, 13]

Approach

  1. Generate the series and keep values from 1 to 20.

Solution

Python
fib = []a, b = 1, 1while a <= 20:    fib.append(a)    a, b = b, a + bprint(fib)
Output
[1, 1, 2, 3, 5, 8, 13]

Learn the concept

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

More list programs

← All 100 List Programs

Factorial of each item

Python Practice › List Programs › Program 77

Factorial of each item

EasyList Programs · 77 of 100

Write a Python program to get the factorial of each item in a list.

Input
[3, 4, 5, 6]
Output
[6, 24, 120, 720]

Approach

  1. math.factorial on each item.

Solution

Python
import mathprint([math.factorial(n) for n in [3, 4, 5, 6]])
Output
[6, 24, 120, 720]

Learn the concept

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

More list programs

← All 100 List Programs

Remove duplicate strings

Python Practice › List Programs › Program 76

Remove duplicate strings

EasyList Programs · 76 of 100

Write a Python program to remove duplicate strings from a list.

Input
["python", "is", "a", "best", "language", "python", "best"]
Output
['python', 'is', 'a', 'best', 'language']

Approach

  1. dict.fromkeys.

Solution

Python
words = ["python", "is", "a", "best", "language", "python", "best"]print(list(dict.fromkeys(words)))
Output
['python', 'is', 'a', 'best', 'language']

Learn the concept

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

More list programs

← All 100 List Programs

Insert an element before each item

Python Practice › List Programs › Program 75

Insert an element before each item

EasyList Programs · 75 of 100

Write a Python program to insert an element before each element of a list.

Input
[3, 5, 7, 8], element "a"
Output
['a', 3, 'a', 5, 'a', 7, 'a', 8]

Approach

  1. A nested comprehension produces the pair for each item.

Solution

Python
items = [3, 5, 7, 8]print([x for item in items for x in ("a", item)])
Output
['a', 3, 'a', 5, 'a', 7, 'a', 8]

Learn the concept

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

More list programs

← All 100 List Programs

Remove duplicate tuples

Python Practice › List Programs › Program 74

Remove duplicate tuples

EasyList Programs · 74 of 100

Write a Python program to remove duplicate tuples from a list.

Input
[(2, 3), (4, 6), (5, 1), (2, 3), (7, 9), (5, 1)]
Output
[(2, 3), (4, 6), (5, 1), (7, 9)]

Approach

  1. Tuples are hashable, so dict.fromkeys works and keeps order.

Solution

Python
data = [(2, 3), (4, 6), (5, 1), (2, 3), (7, 9), (5, 1)]print(list(dict.fromkeys(data)))
Output
[(2, 3), (4, 6), (5, 1), (7, 9)]

Learn the concept

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

More list programs

← All 100 List Programs

Every other item

Python Practice › List Programs › Program 73

Every other item

EasyList Programs · 73 of 100

Write a Python program to create a list from every second item.

Input
[3, 5, 7, 8, 2, 9, 3, 5, 11]
Output
[3, 7, 2, 3, 11]

Approach

  1. [::2].

Solution

Python
print([3, 5, 7, 8, 2, 9, 3, 5, 11][::2])
Output
[3, 7, 2, 3, 11]

Learn the concept

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

More list programs

← All 100 List Programs