Get startedGet started for free

Finding an element

Using the right data structure can significantly increase the performance of your code. For example, if you want to find a certain element in the data, you might want to choose between list and set. In this exercise, you will implement performance tests with pytest to compare the speed of the in operator applied correspondingly to the two data structures: list and set. The pytest package has already been imported.

This exercise is part of the course

Introduction to Testing in Python

View Course

Exercise instructions

  • Pass benchmark as an argument into the test functions.
  • Then call benchmark() in the test functions passing find() as the first argument.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

def create_list():
    return [i for i in range(1000)]
def create_set():
    return set([i for i in range(1000)])
def find(it, el=50):
    return el in it

# Write the performance test for a list
def test_list(____):
    ____(____, ____)

# Write the performance test for a set
def test_set(____):
    ____(____, ____)
Edit and Run Code