Insert a sublist at an index

Python Practice › List Programs › Program 86

Insert a sublist at an index

EasyList Programs · 86 of 100

Write a Python program to insert a sublist into a list at a given index.

Input
[4, 6, 8, 2, 3, 5], sublist [5, 2, 6], index 3
Output
[4, 6, 8, [5, 2, 6], 2, 3, 5]
[4, 6, 8, 5, 2, 6, 2, 3, 5]

Approach

  1. insert adds the sublist as one item; slice assignment adds its items.

Solution

Python
items = [4, 6, 8, 2, 3, 5]nested = items.copy()nested.insert(3, [5, 2, 6])print(nested) flat = items.copy()flat[3:3] = [5, 2, 6]print(flat)
Output
[4, 6, 8, [5, 2, 6], 2, 3, 5]
[4, 6, 8, 5, 2, 6, 2, 3, 5]

Line by line

Line 3

The sublist becomes one element.

Line 7

Assigning to the empty slice [3:3] splices the items in.

Learn the concept

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

More list programs

← All 100 List Programs

Remove duplicates with a set

Python Practice › List Programs › Program 85

Remove duplicates with a set

EasyList Programs · 85 of 100

Write a Python program to remove duplicates from a list using a set.

Input
[2, 5, 7, 8, 2, 3, 4, 12, 5, 6]
Output
[2, 3, 4, 5, 6, 7, 8, 12]
[2, 5, 7, 8, 3, 4, 12, 6]

Approach

  1. set() removes duplicates; use sorted(..., key=items.index) to restore the original order.

Solution

Python
items = [2, 5, 7, 8, 2, 3, 4, 12, 5, 6]unique = set(items)print(sorted(unique))print(sorted(unique, key=items.index))
Output
[2, 3, 4, 5, 6, 7, 8, 12]
[2, 5, 7, 8, 3, 4, 12, 6]

Line by line

Line 4

Sorting by each value’s first index restores the original order.

Learn the concept

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

More list programs

← All 100 List Programs

Dictionary from two lists

Python Practice › List Programs › Program 84

Dictionary from two lists

EasyList Programs · 84 of 100

Write a Python program to create a dictionary from a list of keys and a list of values.

Input
["a", "b", "c", "d", "e"], [234, 123, 456, 343, 223]
Output
{'a': 234, 'b': 123, 'c': 456, 'd': 343, 'e': 223}

Approach

  1. dict(zip(keys, values)).

Solution

Python
keys = ["a", "b", "c", "d", "e"]values = [234, 123, 456, 343, 223]print(dict(zip(keys, values)))
Output
{'a': 234, 'b': 123, 'c': 456, 'd': 343, 'e': 223}

Learn the concept

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

More list programs

← All 100 List Programs

Remove n elements from the left or right

Python Practice › List Programs › Program 83

Remove n elements from the left or right

EasyList Programs · 83 of 100

Write a Python program to remove n elements from the left and from the right of a list.

Input
[2, 5, 7, 9, 3, 4], n = 1 and 2
Output
remove 1 from left: [5, 7, 9, 3, 4] | from right: [2, 5, 7, 9, 3]
remove 2 from left: [7, 9, 3, 4] | from right: [2, 5, 7, 9]

Approach

  1. items[n:] drops from the left, items[:-n] from the right.

Solution

Python
items = [2, 5, 7, 9, 3, 4]for n in [1, 2]:    print(f"remove {n} from left:", items[n:], f"| from right:", items[:-n])
Output
remove 1 from left: [5, 7, 9, 3, 4] | from right: [2, 5, 7, 9, 3]
remove 2 from left: [7, 9, 3, 4] | from right: [2, 5, 7, 9]

Learn the concept

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

More list programs

← All 100 List Programs

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