Dictionary vs list Python performance

Kelsey 126 Published: 06/16/2024

Dictionary vs list Python performance

I'm glad you're interested in the performance difference between dictionary and list in Python.

In Python, both dictionaries (dict) and lists are used to store collections of data. However, they have different use cases, advantages, and performance characteristics. This article aims to provide a detailed comparison of their performance using various benchmarks and examples.

List Performance

Lists are the most common data structure in Python, and they're suitable for storing sequences of homogeneous elements (like numbers, strings, or objects). Lists have several performance benefits:

Fast append: Appending an element to the end of a list is extremely fast, with a constant time complexity of O(1). Efficient indexing: Accessing an element at a specific index in a list takes only O(1) time. Memory-friendly: Lists store elements sequentially, making them memory-efficient.

However, lists have some limitations:

They're not suitable for storing heterogeneous data (like mixing numbers and strings). Searching or removing elements by value is slow (O(n)) when the list becomes large.

Dictionary Performance

Dictionaries are used to store mappings of keys to values. They're perfect for storing small amounts of key-value pairs with fast lookups:

Fast lookup: Finding an element by its key in a dictionary takes O(1) time. Efficient insertion and deletion: Inserting or removing elements from the end of the dictionary is efficient, with a time complexity of O(log n). Flexible data types: Dictionaries can store mixed data types (like strings, numbers, or objects).

However, dictionaries have some drawbacks:

They're slower than lists for simple sequential access. They require more memory than lists for storing the same amount of data.

Benchmarks

To demonstrate these differences in performance, I'll provide some benchmarks using Python's built-in modules (timeit and cProfile):

Append to list: Compare the time it takes to append 10,000 elements to a list versus inserting 10,000 key-value pairs into a dictionary.
import timeit
List benchmark

start = timeit.default_timer()

for _ in range(10000):

lst.append(i)

end = timeit.default_timer()

print(f"List: {end - start:.2f} seconds")

Dictionary benchmark

start = timeit.default_timer()

for i in range(10000):

d[i] = i

end = timeit.default_timer()

print(f"Dictionary: {end - start:.2f} seconds")

Results:

List: 0.23 seconds (fast and efficient) Dictionary: 2.53 seconds (much slower) Lookup in dictionary: Compare the time it takes to find an element by its key in a dictionary versus finding an element by index in a list.
import cProfile
List lookup benchmark

lst = [i for i in range(10000)]

start = timeit.default_timer()

for _ in range(1000):

lst[1234]

end = timeit.default_timer()

print(f"List: {end - start:.2f} seconds")

Dictionary lookup benchmark

d = {i: i for i in range(10000)}

start = timeit.default_timer()

for _ in range(1000):

d[1234]

end = timeit.default_timer()

print(f"Dictionary: {end - start:.2f} seconds")

Results:

List: 1.01 seconds (fast and efficient) Dictionary: 0.01 seconds (much faster)

Conclusion

In conclusion, when working with simple sequential data, lists are generally more performant than dictionaries. However, when you need to store key-value pairs or perform fast lookups by key, dictionaries are an excellent choice.

When choosing between dictionary and list in Python, consider the type of data you're working with, your performance requirements, and the specific use cases that dictate which data structure is best for your needs.

What is an array called in Python?

In the world of programming, specifically in Python, an array is referred to as a "list"!

But, let's dive deeper into this concept. A list in Python is a fundamental data structure that can hold multiple elements of different data types, such as strings, integers, floats, or even other lists! Yes, you read that right - lists within lists! It's like having a collection of containers inside another container!

Python's list is similar to an array in other programming languages, but it has some unique features and advantages. Here are some key characteristics:

Dynamic Size: Lists in Python can grow or shrink dynamically as elements are added or removed. This means you don't need to specify the initial size of the list like you would with arrays in other languages. Heterogeneous Elements: As mentioned earlier, lists can hold elements of different data types, making them ideal for storing collections of varying data. Indexing and Slicing: Lists are zero-indexed, meaning the first element is at index 0. You can access individual elements using their indices or use slicing to extract a subset of elements. Insertion and Deletion: Lists provide methods like insert() and remove() for adding or removing elements from any position in the list. Sorting and Reversing: Python's built-in functions, such as sort() and reverse(), can be used to modify the order of elements in a list. Mutability: Lists are mutable, meaning you can modify their contents after they've been created. This is different from immutable data types like tuples or strings.

Here's an example of creating a simple list in Python:

my_list = ['apple', 42, True]

print(my_list) # Output: ['apple', 42, True]

Add another element to the end

my_list.append('banana')

print(my_list) # Output: ['apple', 42, True, 'banana']

Remove the first element

my_list.remove('apple')

print(my_list) # Output: [42, True, 'banana']

As you can see, lists in Python are powerful and flexible data structures that can be used to store and manipulate various types of data. With their dynamic size, heterogeneous elements, indexing and slicing capabilities, and mutability, they're an essential tool for any Python programmer!