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

Keep sublists that are not repeated

Python Practice › List Programs › Program 72

Keep sublists that are not repeated

EasyList Programs · 72 of 100

Write a Python program to keep only the sublists that appear once.

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

Approach

  1. Count tuples (lists are unhashable), keep those with count 1.

Solution

Python
from collections import Counter data = [[1, 2], [3, 5], [1, 2], [6, 7]]counts = Counter(tuple(x) for x in data)print([x for x in data if counts[tuple(x)] == 1])
Output
[[3, 5], [6, 7]]

Line by line

Line 4

Converting each sublist to a tuple makes it countable.

Learn the concept

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

More list programs

← All 100 List Programs

Are all elements unique?

Python Practice › List Programs › Program 71

Are all elements unique?

EasyList Programs · 71 of 100

Write a Python program to check whether all elements of a list are unique.

Input
[2, 5, 6, 7, 4, 11, 2, 4, 66, 21, 22, 3]
Output
False

Approach

  1. A set has the same length only if nothing repeated.

Solution

Python
items = [2, 5, 6, 7, 4, 11, 2, 4, 66, 21, 22, 3]print(len(set(items)) == len(items))
Output
False

Learn the concept

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

More list programs

← All 100 List Programs

Remove duplicate dictionaries

Python Practice › List Programs › Program 70

Remove duplicate dictionaries

EasyList Programs · 70 of 100

Write a Python program to remove duplicate dictionaries from a list, keeping the first of each.

Input
[{"name": "john"}, {"city": "mumbai"}, {"Python": "language"}, {"name": "john"}]
Output
[{'name': 'john'}, {'city': 'mumbai'}, {'Python': 'language'}]

Approach

  1. Dicts are not hashable, so compare with the dicts already kept.

Solution

Python
data = [{"name": "john"}, {"city": "mumbai"}, {"Python": "language"}, {"name": "john"}]result = []for d in data:    if d not in result:        result.append(d)print(result)
Output
[{'name': 'john'}, {'city': 'mumbai'}, {'Python': 'language'}]

Line by line

Line 4

in compares dicts by their contents.

Learn the concept

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

More list programs

← All 100 List Programs

Is a list sorted?

Python Practice › List Programs › Program 69

Is a list sorted?

EasyList Programs · 69 of 100

Write a Python program to check whether a list is sorted in ascending order.

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

Approach

  1. Every item must be less than or equal to the next one.

Solution

Python
def is_sorted(items):    return all(a <= b for a, b in zip(items, items[1:])) print(is_sorted([1, 2, 3, 5, 7, 8, 9]), is_sorted([3, 5, 1, 6, 8, 2, 4]))
Output
True False

Line by line

Line 2

Checking neighbours is faster than comparing with sorted(items).

Learn the concept

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

More list programs

← All 100 List Programs

Elements at several indexes

Python Practice › List Programs › Program 68

Elements at several indexes

EasyList Programs · 68 of 100

Write a Python program to get the elements at a list of indexes.

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

Approach

  1. Look up each index.

Solution

Python
items = [2, 3, 4, 7, 8, 1, 5, 6, 2, 1, 8, 2]indexes = [0, 3, 5, 6]print([items[i] for i in indexes])
Output
[2, 7, 1, 5]

Learn the concept

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

More list programs

← All 100 List Programs

Count integers in a mixed list

Python Practice › List Programs › Program 67

Count integers in a mixed list

EasyList Programs · 67 of 100

Write a Python program to count the integers in a mixed list.

Input
["Hello", 45, "sqa", 23, 5, "Tools", 20]
Output
4

Approach

  1. Count items where isinstance(x, int).

Solution

Python
data = ["Hello", 45, "sqa", 23, 5, "Tools", 20]print(sum(isinstance(x, int) for x in data))
Output
4

Line by line

Line 2

Each True counts as 1 when summed.

Learn the concept

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

More list programs

← All 100 List Programs