Check if an element exists

Python Practice › List Programs › Program 39

Check if an element exists

EasyList Programs · 39 of 100

Write a Python program to check whether an element exists in a list.

Input
[3, 9, 27], check 9 and 4
Output
9 exists
4 does not exist

Approach

  1. Use in.

Solution

Python
numbers = [3, 9, 27]for x in [9, 4]:    print(x, "exists" if x in numbers else "does not exist")
Output
9 exists
4 does not exist

Learn the concept

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

More list programs

← All 100 List Programs

Replace the first and last items

Python Practice › List Programs › Program 38

Replace the first and last items

EasyList Programs · 38 of 100

Write a Python program to replace the first and last numbers of a list with “SQA” and “Tools”.

Input
[12, 32, 33, 5, 4, 7]
Output
['SQA', 32, 33, 5, 4, 'Tools']

Approach

  1. Assign to index 0 and index -1.

Solution

Python
numbers = [12, 32, 33, 5, 4, 7]numbers[0] = "SQA"numbers[-1] = "Tools"print(numbers)
Output
['SQA', 32, 33, 5, 4, 'Tools']

Learn the concept

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

More list programs

← All 100 List Programs

Convert a string to a list

Python Practice › List Programs › Program 37

Convert a string to a list

EasyList Programs · 37 of 100

Write a Python program to convert a string into a list of words and a list of characters.

Input
"learn python"
Output
['learn', 'python']
['l', 'e', 'a', 'r', 'n', ' ', 'p', 'y', 't', 'h', 'o', 'n']

Approach

  1. split() for words, list() for characters.

Solution

Python
text = "learn python"print(text.split())print(list(text))
Output
['learn', 'python']
['l', 'e', 'a', 'r', 'n', ' ', 'p', 'y', 't', 'h', 'o', 'n']

Learn the concept

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

More list programs

← All 100 List Programs

Numbers that appear exactly once

Python Practice › List Programs › Program 36

Numbers that appear exactly once

EasyList Programs · 36 of 100

Write a Python program to get the numbers that appear exactly once in a list.

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

Approach

  1. Count with Counter and keep counts of 1.

Solution

Python
from collections import Counter numbers = [1, 2, 2, 3, 4, 4, 5]counts = Counter(numbers)print([n for n in numbers if counts[n] == 1])print(list(dict.fromkeys(numbers)))
Output
[1, 3, 5]
[1, 2, 3, 4, 5]

Line by line

Line 5

Values that occur only once.

Line 6

Every distinct value (duplicates removed), for comparison.

Learn the concept

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

More list programs

← All 100 List Programs

Keys and values from a list of dicts

Python Practice › List Programs › Program 35

Keys and values from a list of dicts

EasyList Programs · 35 of 100

Write a Python program to get all keys and all values from a list of single-item dictionaries.

Input
[{"a": 12}, {"b": 34}, {"c": 23}, {"d": 11}, {"e": 15}]
Output
['a', 'b', 'c', 'd', 'e']
[12, 34, 23, 11, 15]

Approach

  1. Loop over each dict’s items.

Solution

Python
data = [{"a": 12}, {"b": 34}, {"c": 23}, {"d": 11}, {"e": 15}]keys = [k for d in data for k in d]values = [v for d in data for v in d.values()]print(keys)print(values)
Output
['a', 'b', 'c', 'd', 'e']
[12, 34, 23, 11, 15]

Learn the concept

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

More list programs

← All 100 List Programs

Pair up two lists as sublists

Python Practice › List Programs › Program 34

Pair up two lists as sublists

EasyList Programs · 34 of 100

Write a Python program to combine items at the same position of two lists into sublists.

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

Approach

  1. zip pairs items; convert each pair to a list.

Solution

Python
a = [3, 5, 7, 8, 9]b = [1, 4, 3, 6, 2]print([[x, y] for x, y in zip(a, b)])
Output
[[3, 1], [5, 4], [7, 3], [8, 6], [9, 2]]

Learn the concept

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

More list programs

← All 100 List Programs

Reverse each string in a list

Python Practice › List Programs › Program 33

Reverse each string in a list

EasyList Programs · 33 of 100

Write a Python program to reverse each string in a list.

Input
["Sqa", "Tools", "Online", "Learning", "Platform"]
Output
['aqS', 'slooT', 'enilnO', 'gninraeL', 'mroftalP']

Approach

  1. Apply [::-1] to every item.

Solution

Python
words = ["Sqa", "Tools", "Online", "Learning", "Platform"]print([w[::-1] for w in words])
Output
['aqS', 'slooT', 'enilnO', 'gninraeL', 'mroftalP']

Learn the concept

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

More list programs

← All 100 List Programs

Difference between two lists

Python Practice › List Programs › Program 32

Difference between two lists

EasyList Programs · 32 of 100

Write a Python program to get the items in the first list that are not in the second.

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

Approach

  1. Keep items of list1 not in list2.

Solution

Python
a = [1, 2, 3, 4, 5]b = [2, 4, 6]print([x for x in a if x not in b])print(sorted(set(a) ^ set(b)))
Output
[1, 3, 5]
[1, 3, 5, 6]

Line by line

Line 3

Items only in a, in their original order.

Line 4

The symmetric difference gives items in exactly one of the lists.

Learn the concept

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

More list programs

← All 100 List Programs

Join elements with a special character

Python Practice › List Programs › Program 31

Join elements with a special character

EasyList Programs · 31 of 100

Write a Python program to merge all elements of a list into one string using a special character.

Input
["sqa", "tools", 2026, "python"], separator "#"
Output
sqa#tools#2026#python

Approach

  1. Convert each item to str and join with the separator.

Solution

Python
items = ["sqa", "tools", 2026, "python"]print("#".join(str(x) for x in items))
Output
sqa#tools#2026#python

Line by line

Line 2

join only accepts strings, so the number is converted first.

Learn the concept

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

More list programs

← All 100 List Programs

Second smallest number

Python Practice › List Programs › Program 30

Second smallest number

EasyList Programs · 30 of 100

Write a Python program to find the second smallest number in a list.

Input
[10, 4, 32, 4, 18]
Output
10

Approach

  1. Sort the unique values and take index 1.

Solution

Python
numbers = [10, 4, 32, 4, 18]print(sorted(set(numbers))[1])
Output
10

Learn the concept

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

More list programs

← All 100 List Programs