Contents

List drills

Drills to practice working with lists in Python.

Basics

Solve the below tasks and state their time and space complexities.

Define a list.

A list is a finite, ordered, and mutable sequence of elements.

Create a list a containing the letters a, b, and c.

1
a = list("abc")

Append z.

1
a.append("z")

Insert x at the second position.

1
a.insert(1, "x")

Append the characters m, n.

1
a.extend("mn")

Remove the first occurrence of x from the list.

1
a.remove("x")

Remove the second to last element from the list.

1
del a[-2]

Remove and return the last item.

1
a.pop()

Remove and return the second item.

1
a.pop(1)

Check whether c is in the list.

1
"c" in a

Return the index of c.

1
a.index("c")

Count occurrences of ‘c’.

1
a.count("c")

Sort the list in place.

1
a.sort()

Insert ‘z’ in the first position.

1
a.insert(0, "z")

Replace the just inserted z with ‘k’

1
a[0] = "k"

Create a sorted copy of the list.

1
sorted(a)

Reverse the order in place.

1
a.reverse()

Create a reversed iterator.

1
reversed(a)

Delete all elements from the list.

1
a.clear()

Deep and shallow copies

1
a = [1, 2, [3]]

Create shallow copies b, c, d, and e of a, all in different ways.

1
2
3
4
5
6
import copy

b = list(a)
c = a[:]
d = a.copy()
e = copy.copy(a)

Check that the new lists are indeed shallow copies.

1
all(a is not copy and a[2] is copy[2] for copy in [b, c, d])
True

Create a deep copy e of a.

1
e = copy.deepcopy(a)

Check that the new list is a deep copy.

1
e is not a and e[2] is not a[2]

List comprehensions

1
2
colors = ["blue", "yellow"]
sizes = "SML"

Reproduce the output of the below using a list comprehension.

results = []
for color in colors:
    for size in sizes:
        results.append((color, size))
results
1
2
results = [(color, size) for color in colors for size in sizes]
results

Create a copy of results sorted by size in ascending order.

1
sorted(results, key=lambda x: x[1], reverse=True)

Summing elements

1
a = [1, 2, 3, 4, 5]

Sum a using the built-in method.

1
sum(a)

Sum the list using a recursive algorithm.

1
2
3
4
5
def rec_sum(items):
    if not items:
        return 0
    head, *tail = items
    return head + rec_sum(tail) if tail else head
8

Sum the list using a for loop.

1
2
3
4
5
6
7
8
def for_sum(items):
    result = 0
    for item in items:
        result += item
    return result


for_sum(a)

Sum the list using a while loop without altering the input.

1
2
3
4
5
6
7
8
9
def while_sum(items):
    result = i = 0
    while i < len(items):
        result += items[i]
        i += 1
    return result


while_sum(a)

Misc.

What is c?

1
2
3
4
5
a = 1
b = 2
c = [a, b]
a = 2
c
[1, 2]

Find the indices of the min and max elements in the list below.

1
a = [1, 3, 4, 9, 9, 10, 2, 4, 2, 33]
1
a.index(min(a)), a.index(max(a))

Removing duplicates

Write an algorithm to remove duplicates from a list while maintaining it’s original order (from Python cookbook recipe 1.10).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def dedupe(items):
    seen = set()
    deduped = []
    for item in items:
        if item not in seen:
            deduped.append(item)
        seen.add(item)
    return deduped


dedupe([1, 1, 2, 3, 2])

Use a generator function to achieve the same.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def dedupe_gen(items):
    seen = set()
    for item in items:
        if item not in seen:
            seen.add(item)
            yield item


for item in dedupe_gen([1, 1, 2, 3, 2]):
    print(item)

Remove duplicates from the below sorted list by updating the original list

1
a = [1, 2, 2, 3, 4, 4, 4, 5, 6, 6, 7]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def dedupe_inplace(items):
    write_index = 1
    for i in range(2, len(items)):
        if items[i] != items[write_index - 1]:
            items[write_index] = items[i]
            write_index += 1
    return items[:write_index]


dedupe_inplace(a)
[1, 2, 3, 4, 5, 6, 7]

Implementing a stack

Implement a stack with push(), pop(), peek(), and is_empty() methods.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Stack:
    def __init__(self):
        self.data = []

    def push(self, item):
        self.data.append(item)

    def pop(self):
        self.data.pop()

    def peek(self):
        return self.data[-1]

    def is_empty(self):
        return len(self.data) == 0

## Implementing a queue

Implement a queue with basic operations enqueue(), dequeue(), and is_empty().

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Queue:
    def __init__(self):
        self.data = []

    def enqueue(self, item):
        self.data.append(item)

    def dequeue(self):
        return self.data.pop(0)

    def is_empty(self):
        return len(self.data) == 0