Open Menu dzosoft
Close Menu dzosoft

   ALL ABOUT COMPUTER SCIENCE AND TECHNOLOGY


                             





Publish perfectly-optimized content in 1-click









 
 
 

How to create and initialize a Python dictionary

 

 
How to create and initialize a Python dictionary
 

This article explains how to create and initialize a dictionary in Python.
A dictionary is made up of key-value pairs. There are several ways to create and initialize a dictionary in Python, as shown below:

 Using the dictionary constructor

The dict() dictionary constructor returns a new dictionary which is initialized from the specified arguments.

⮚ If a mapping object is passed to the dictionary constructor, the dictionary is created with the same key-value pairs as the mapping object.

if __name__ == '__main__':

d = dict(A=1, B=2, C=3) print(d) # {'A': 1, 'B': 2, 'C': 3}



⮚ You can also pass an iterable to the dictionary constructor, where each element of the iterable must be another iterable with two objects.

This returns a new dictionary with the first object of each element as the key and the second object as the corresponding value. Note that if a key is repeated, the resulting dictionary contains the last value of that key.

if __name__ == '__main__':

x = dict(zip(['A', 'B', 'C'], [1, 2, 3])) print(x) # {'A': 1, 'B': 2, 'C': 3}
y = dict([('B', 2), ('A', 1), ('C', 3)]) print(y) # {'A': 1, 'B': 2, 'C': 3}
z = dict({'A': 1, 'B': 2, 'C': 3}) print(z) # {'A': 1, 'B': 2, 'C': 3}



⮚ If no argument is passed to the dictionary constructor, an empty dictionary is created.

if __name__ == '__main__':

d = dict() print(d) # {}


 Using dictionary literal syntax

Dictionaries can also be created by enclosing a comma-separated list of key: value pairs in braces.

if __name__ == '__main__':

d = {'A': 1, 'B': 2, 'C': 3} print(d) # {'A': 1, 'B': 2, 'C': 3}


If no key-value pair is passed, an empty dictionary is created.

if __name__ == '__main__':

d = {} print(d) # {}


 Using fromkeys() function

If you want to initialize all dictionary keys with a default value, you can use the fromkeys() function.

if __name__ == '__main__':

x = dict.fromkeys(['A', 'B', 'C'], 0) print(x) # {'A': 0, 'B': 0, 'C': 0}


If no default value is specified, the dictionary is initialized with all values ​​as None.

if __name__ == '__main__':

x = dict.fromkeys(['A', 'B', 'C']) print(x) # {'A': None, 'B': None, 'C': None}


Note that if the default value is editable, it may cause unexpected results. For example, in the following program, all keys are mapped to the same list. This means that any change made to one value will be reflected in all values ​​in the dictionary.

if __name__ == '__main__':

x = dict.fromkeys(['A', 'B', 'C'], []) x.get('A').append(1)
print(x) # {'A': [1], 'B': [1], 'C': [1]}


It might also interest you


How to install Python on Windows

How to write your first Python program

How to lock PC with voice command using Python

How to connect MySQL database using Connector/Python

How to draw shapes with python

How to create read and write to a file in python

How to create your own personal assistant using ChatGPT and Python


Leave comment
          

Save nickname and email in this browser for the next time.



Loading...     
close publicity
TRAIN INTENSELY LIKE NEVER BEFORE AND PUSH YOUR LIMITS!