Python Basics: dict and set

Keywords: Python Lambda

The reason why dictionaries and collections are efficient is that the internal structure is a hash table.

On average, the time complexity of inserting, searching and deleting is O(1)

Performance comparison with list in search scenario

Suppose there is a list of 100000 products:

import time
id = [x for x in range(0, 100000)]
price = [x for x in range(200000, 300000)]
products = list(zip(id, price))
# [(0, 200000), (1, 200001)....(99999, 299999)]

To figure out how many different prices there are in total, use the list and set as the data structures to compare the performance.

Use list as data structure:

# # Time to calculate list version

# list version
def find_unique_price_using_list(products):
    unique_price_list = []
    for _, price in products: # A
        if price not in unique_price_list: #B
    return len(unique_price_list)

start_using_list = time.perf_counter()
end_using_list = time.perf_counter()
print("time elapse using list: {}".format(end_using_list - start_using_list))
#time elapse using list: 53.206719899999996


Use set as data structure:

# # Time to calculate collection version
# set version
def find_unique_price_using_set(products):
    unique_price_set = set()
    for _, price in products:
    return len(unique_price_set)     

start_using_set = time.perf_counter()
end_using_set = time.perf_counter()
print("time elapse using set: {}".format(end_using_set - start_using_set))
#time elapse using set: 0.009022799999996778


It can be seen from the results that the performance difference is very large, and it is very important to use the appropriate data structure.

Dict and Set foundation

  1. Index operation not supported for collection
  2. Determine whether the element uses the in operator in dict/set
dict1 = {'a':1,'b':2}
print('a' in dict1) #True
print(1 in dict1)   #False
set1 = {'a','b','c'}
print(1 in set1)   #False
print('b' in set1) #True

3. The pop() method of a collection is to return an element randomly and delete the element in the collection

4. Sorting of sets and dictionaries

#Dictionary ranking
d = {'b': 1, 'a': 2, 'c': 10}
d_sorted_by_key = sorted(d.items(), key=lambda x: x[0]) # Sort dictionary keys in ascending order
d_sorted_by_value = sorted(d.items(), key=lambda x: x[1]) # Sort dictionary values in ascending order
[('a', 2), ('b', 1), ('c', 10)]
[('b', 1), ('a', 2), ('c', 10)]

#Set sort
s = {3, 4, 2, 1}
sorted(s) # Sort the elements of a collection in ascending order
[1, 2, 3, 4]


Geek time: Python core technology and practice column

Posted by cornercuttinjoe on Fri, 08 Nov 2019 10:04:47 -0800