60 Questions to Test Your Knowledge of Python Lists
By: Chris I.
Tags: Programming, Software Engineering, Software Development, Python, Data Structures

Questions 1–10:

1. Check if a list contains an element

The in operator will return True if a specific element is in a list.

li = [1,2,3,'a','b','c'] 'a' in li

#=> True

2. How to iterate over 2+ lists at the same time

You can zip() lists and then iterate over the zip object. A zip object is an iterator of tuples.

Below we iterate over 3 lists simultaneously and interpolate the values into a string.

name = ['Snowball', 'Chewy', 'Bubbles', 'Gruff']

animal = ['Cat', 'Dog', 'Fish', 'Goat']

age = [1, 2, 2, 6] z = zip(name, animal, age)

z #=> <zip at 0x111081e48> for name,animal,age in z:

print("%s the %s is %s" % (name, animal, age))



#=> Snowball the Cat is 1

#=> Chewy the Dog is 2

#=> Bubbles the Fish is 2

#=> Gruff the Goat is 6

3. When would you use a list vs dictionary?

Lists and dictionary generally have slightly different use cases but there is some overlap.

The general rule of algorithm questions I’ve come to is that if you can use both, use a dictionary because lookups are faster.

List

Use a list if you need to store the order of something.

Ie: id’s of database records in the order they’ll be displayed.

ids = [23,1,7,9]

While both lists and dictionaries are ordered as of python 3.7, a list allows duplicate values while a dictionary doesn’t allow duplicate keys.

Dictionary

Use a dictionary if you want to count occurrences of something. Like the number of pets in a home.

pets = {'dogs':2,'cats':1,'fish':5}

Each key can only exist once in a dictionary. Note that keys can also be other immutable data structures like tuples. Ie: {('a',1):1, ('b',2):1} .

4. Is a list mutable?

Yes. Notice in the code below how the value associated with the same identifier in memory has not changed.

x = [1]

print(id(x),':',x) #=> 4501046920 : [1] x.append(5)

x.extend([6,7])

print(id(x),':',x) #=> 4501046920 : [1, 5, 6, 7]

5. Does a list need to be homogeneous?

No. Different types of object can be mixed together in a list.

a = [1,'a',1.0,[]]

a #=> [1, 'a', 1.0, []]

6. What is the difference between append and extend?

.append() adds an object to the end of a list.

a = [1,2,3]

a.append(4)

a #=> [1, 2, 3, 4]

This also means appending a list adds that whole list as a single element, rather than appending each of its values.

a.append([5,6])

a #=> [1, 2, 3, 4, [5, 6]]

.extend() adds each value from a 2nd list as its own element. So extending a list with another list combines their values.

b = [1,2,3]

b.extend([5,6])

b #=> [1, 2, 3, 5, 6]

7. Do python lists store values or pointers?

Python lists don’t store values themselves. They store pointers to values stored elsewhere in memory. This allows lists to be mutable.

Here we initialize values 1 and 2 , then create a list including the values 1 and 2 .

print( id(1) ) #=> 4438537632

print( id(2) ) #=> 4438537664 a = [1,2,3]

print( id(a) ) #=> 4579953480 print( id(a[0]) ) #=> 4438537632

print( id(a[1]) ) #=> 4438537664

Notice how the list has its own memory address. But 1 and 2 in the list point to the same place in memory as the 1 and 2 we previously defined.

8. What does “del” do?

del removes an item from a list given its index.

Here we’ll remove the value at index 1.

a = ['w', 'x', 'y', 'z']

a #=> ['w', 'x', 'y', 'z'] del a[1] a #=> ['w', 'y', 'z']

Notice how del does not return the removed element.

9. What is the difference between “remove” and “pop”?

.remove() removes the first instance of a matching object. Below we remove the first b .

a = ['a', 'a', 'b', 'b', 'c', 'c']

a.remove('b')

a #=> ['a', 'a', 'b', 'c', 'c']

.pop() removes an object by its index.

The difference between pop and del is that pop returns the popped element. This allows using a list like a stack.

a = ['a', 'a', 'b', 'b', 'c', 'c']

a.pop(4) #=> 'c'

a #=> ['a', 'a', 'b', 'b', 'c']

By default, pop removes the last element from a list if an index isn’t specified.

10. Remove duplicates from a list

If you’re not concerned about maintaining the order of a list, then converting to a set and back to a list will achieve this.