Sobes.tech
Junior

Which is faster, a dictionary or a list?

sobes.tech AI

Answer from AI

Dictionary is faster for key-based lookup (O(1) on average), list is faster for appending/removing at the end (O(1)). For lookup by value or inserting/removing at an arbitrary position, list is slower (O(n)).

# Lookup in dictionary
my_dict = {i: i for i in range(100000)}
# Fast
print(1 in my_dict)
# Lookup in list
my_list = list(range(100000))
# Slower
print(1 in my_list)
Which is faster, a dictionary or a list? — Python - sobes.tech