**Python Dictionary is an unordered collection of key:value pairs**. As Dictionaries are mutable meaning key, value pair can be added or removed from these. For doing this, Python have a number of Methods/Operations. I’ve put together an article explaining all of Dictionary Methods you can see that article here – **Dictionary Methods/Operations in Python**.

Let’s first try to understand **What’s meaning of Complexity in Programming** and Why you as a Python Developer need to care about this while writing code.

Table of Contents

## What is Complexity in Programming?

**Complexity or O(n) is measurement of How many items needed to be considered for execute something.** From daily life you can think this of as brushing your teeth, How many steps items you need to do brushing can be consider Complexity.(Quite Simple!!)**Why Programmer need to consider Complexity?** **Complexity needs to be considered by programmer while writing code as sometimes a piece of code need to be executed fast or sometime slower.** For example – If your writing code for a Self Driving Car then you want code to be executed faster so that action(Turning Left/Right) can be taken quickly.

### Defining Complexity Mathematically O(n)

- O(1) means in constant time – independent of the number of items.
- O(N) means in proportion to the number of items.
- O(log N) means a time proportional to log(N)

Basically any ‘O’ notation means an operation will take time up to a maximum of **k*f(N)**

where: **k is a constant multiplier and f() is a function that depends on N**

## Table containing Dictionary Operations/Methods Complexity

Just keep in mind that **d is a dicitonary, i is some key and v is some value** in below table.

Operation/Method | Example | Complexity |
---|---|---|

Index | d[i] | O(1) |

Store | d[i] = v | O(1) |

Length | len(d) | O(1) |

Delete | del d[i] | O(1) |

get/setdefault | d.get[i] | O(1) |

Pop | d.pop(v) | O(1) |

Pop item | d.popitem() | O(1) |

Clear | d.clear() | O(1) |

View | d.keys() | O(1) |

Construction | dict(….) | O(len(……)) |

Iteration | for i in d: | O(N) |

## No Comments

Leave a comment Cancel