'What is the time complexity of adding new key-value pair to a dictionary in Python?
What's the time complexity of adding a new key-value pair like so:
d = dict()
d.update({'key':'value'})
Can't find it in here https://wiki.python.org/moin/TimeComplexity
Solution 1:[1]
python dict is hash table
Below is table of "Time complexity in big O notation" (taken from the same link above)
Algorithm Average Worst case
Space O(n)1 O(n)
Search O(1) O(n)
Insert O(1) O(n)
Delete O(1) O(n)
Solution 2:[2]
Since "Set item" is listed as O(1) average case and O(n) worst case, I believe you can assume this is also the complexity for update().
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | balderman |
| Solution 2 | Code-Apprentice |
