{"id":5581,"date":"2023-10-28T17:00:29","date_gmt":"2023-10-28T11:30:29","guid":{"rendered":"https:\/\/python-programs.com\/?p=5581"},"modified":"2023-11-10T12:05:01","modified_gmt":"2023-11-10T06:35:01","slug":"what-is-a-dictionary-in-python-and-why-do-we-need-it","status":"publish","type":"post","link":"https:\/\/python-programs.com\/what-is-a-dictionary-in-python-and-why-do-we-need-it\/","title":{"rendered":"What is a Dictionary in Python and why do we need it"},"content":{"rendered":"
Dictionaries are Python\u2019s implementation of an associative list, which\u00a0may be a\u00a0<\/span>arrangement\u00a0<\/span>. A dictionary\u00a0may be a\u00a0<\/span>collection of key-value pairs that are stored together. A key and its value are represented by each key-value pair.<\/p>\n This is a dictionary example that contains cricket names as keys and the highest score as\u00a0values. Elements are stored as key-value pairs in the dictionary, where each value is mapped to one key. It is also called a hash table or an associative array.<\/p>\n <\/p>\n There are four elements in the above dictionary, i.e. four key-value pairs.<\/p>\n As a dictionary, it holds the elements in key-value mapping format and uses hashing internally; as a result, we can easily retrieve a value from the dictionary by its key. In the best-case scenario, its complexity is O(1), while in the worst-case scenario, it can be O(n).<\/p>\n We can create dictionary by two ways i.e two types of syntaxes.<\/p>\n It will generate an empty dictionary. Let us create a sample dictionary and print the key value pairs in it<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Keys in the dictionary are always unique, and they must be of an immutable data type, such as strings, numbers, or tuples. For example, suppose we make a dictionary with duplicate keys and print its contents.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n A dictionary’s values can be of any type.<\/p>\n Let’s make a dictionary where the key is an integer and the value is a list of strings.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Using the [] operator on the dictionary object, we can access a specific item\/pair in a dictionary. If we use the dictionary object’s operator [] and pass a key, it will return its value as shown below.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Now when we pass a key which does not exist in the dictionary, a KeyError is returned<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Dictionaries are Python\u2019s implementation of an associative list, which\u00a0may be a\u00a0arrangement\u00a0. A dictionary\u00a0may be a\u00a0collection of key-value pairs that are stored together. A key and its value are represented by each key-value pair. Dictionary in Python and why do we need it Dictionary in Python Use of Dictionary Creating a new Dictionary The most important …<\/p>\nDictionary in Python and why do we need it<\/h2>\n
\n
1)Dictionary in Python<\/h3>\n
\n
2)Use of Dictionary<\/h3>\n
3)Creating a new Dictionary<\/h3>\n
\n
\nTransfer the key-value pairs in curly braces to build a dictionary of objects. A colon will also be used to divide the key and value in each pair (:).<\/p>\n# given dictionary\r\ndictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}\r\n# printing the dictionary\r\nprint(dictionary)\r\n<\/pre>\n
{'This': 100, 'is': 200, 'BTechGeeks': 300}<\/pre>\n
4)The most important things to know about keys in the dictionary<\/h3>\n
\nThis means that once a key-value pair is added to the dictionary, we cannot change the key itself, but we can change the value associated with it.<\/p>\n# given dictionary\r\ndictionary = {'This': 100, 'is': 200, 'This': 300}\r\n# printing the dictionary\r\nprint(dictionary)\r\n<\/pre>\n
{'This': 300, 'is': 200}<\/pre>\n
5)The most important things to know about values in the dictionary<\/h3>\n
# given dictionary\r\ndictionary = {1: [\"HELLO\", \"THIS\", 453], \r\n 2: [\"BTECHGEEKS\", 345, 87, 4.5], \r\n 3: [\"PYTHON\", True, 3.2, 100]}\r\n# printing the dictionary\r\nprint(dictionary)\r\n<\/pre>\n
{1: ['HELLO', 'THIS', 453], 2: ['BTECHGEEKS', 345, 87, 4.5], 3: ['PYTHON', True, 3.2, 100]}<\/pre>\n
6)Accessing an element in dictionary<\/h3>\n
# given dictionary\r\ndictionary = {'This': 100, 'is': 200, 'This': 300}\r\n# printing the element value in dictionary\r\nprint(dictionary['is'])\r\n<\/pre>\n
200<\/pre>\n
# given dictionary\r\ndictionary = {'This': 100, 'is': 200, 'BTechGeeks': 300}\r\n# printing the element value in dictionary\r\nprint(dictionary['python'])\r\n<\/pre>\n
Traceback (most recent call last):\r\n File \"\/home\/eb2788937138651d15b1bf1915ff16a5.py\", line 4, in <module>\r\n print(dictionary['python'])\r\nKeyError: 'python'<\/pre>\n
\n