Contents
What is Dictionary in Python?
First of All it is not sequential like List. It is an non-sequential, unordered, redundant and mutable collection as key:value pairs. Keys are always unique but values need not be unique. You use the key to access the corresponding value. Where a list index is always a number, a dictionary key can be a different data type, like a string, integer, float, or even tuples.
Python dictionaries are unordered collections of key-value pairs. They are mutable, meaning you can add, remove, and modify elements after creation. Dictionary keys must be unique and immutable (e.g., strings, numbers, tuples), while values can be of any data type.
The contents of a dict can be written as a series of key:value pairs within braces { }, e.g.
dict = {key1:value1, key2:value2, … }.
The “empty dict” is just an empty pair of curly braces {}.
Differences between String and Dictionary
list = [1,’A’,2,3,2,8.7]
dict = {0:1,1:’A’,2:2,3:3,4:2,5:8.7}
List has numeric index only in sequential order starting from zero.
Dictionary is has keys instead of index which can be numeric, string or tuple. They are unordered but they are always unique.
0 1 2 3 4 # indexes are not there
{‘A’:’A’,’Apple’:253,12:3,13:435,445:34}
Functions for Dictionaries
Method | Syntax | Description | Example |
clear() | dict.clear() | Removes all items from the dictionary. | my_dict = {‘a’: 1, ‘b’: 2}<br>my_dict.clear() |
copy() | dict.copy() | Returns a shallow copy of the dictionary. | my_dict = {‘a’: 1, ‘b’: 2}<br>new_dict = my_dict.copy() |
get() | dict.get(key[, default]) | Returns the value for the specified key. If the key is not found, returns the default value or None. | my_dict = {‘a’: 1, ‘b’: 2}<br>value = my_dict.get(‘a’) |
items() | dict.items() | Returns a view object that displays a list of dictionary’s (key, value) tuple pairs. | my_dict = {‘a’: 1, ‘b’: 2}<br>items = my_dict.items() |
keys() | dict.keys() | Returns a view object that displays a list of all the keys in the dictionary. | my_dict = {‘a’: 1, ‘b’: 2}<br>keys = my_dict.keys() |
values() | dict.values() | Returns a view object that displays a list of all the values in the dictionary. | my_dict = {‘a’: 1, ‘b’: 2}<br>values = my_dict.values() |
pop() | dict.pop(key[, default]) | Removes the item with the specified key and returns its value. If the key is not found, returns default (or raises KeyError if not provided). | my_dict = {‘a’: 1, ‘b’: 2}<br>value = my_dict.pop(‘a’) |
popitem() | dict.popitem() | Removes and returns an arbitrary (key, value) pair from the dictionary. | my_dict = {‘a’: 1, ‘b’: 2}<br>item = my_dict.popitem() |
update() | dict.update([other]) | Updates the dictionary with elements from another dictionary or from an iterable of (key, value) pairs. | my_dict = {‘a’: 1, ‘b’: 2}<br>my_dict.update({‘c’: 3}) |
fromkeys() | dict.fromkeys(seq[, value]) | Creates a new dictionary with keys from the given sequence and values set to a default value (None by default). | keys = [‘a’, ‘b’, ‘c’]<br>my_dict = dict.fromkeys(keys) |
0 Comments