How to create a dictionary in PythonA Python dictionary is stored the data in the pair of key-value. It organizes the data in a unique manner where some specific value exists for some particular key. It is a mutable data-structure; its element can be modified after creation. Before creating a dictionary, we should remember the following points.
Creating a DictionaryThe dictionary is created using the multiple key-value pair, which enclosed within the curly brackets {}, and each key is separated from its value by the colon (:). The syntax is given below. Syntax: In the above dictionary, the Name, Age, Rollnu are the keys which immutable object and James, 25, 0090001 are its values. Let’s see the following example. Example – Output: <class 'dict'> printing Employee data .... {'Name': 'John', 'Age': 10, 'Result': 'Passed', 'Rollno': '009001'} The empty curly bracket {} is used to create empty dictionary. We can also create dictionary using the built-in dict() function. Let’s understand the following example. Example – Output: Empty Dictionary is: {} Create Dictionary by using the dict() method : {1: 'Hello', 2: 'Hi', 3: 'Hey'} Dictionary with each item as a pair: {'Devansh': 90014, 'Arun': 90015} The dictionary is mostly used to store a large amount of data where we can access any value by its key. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263625.html