Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

python

How to copy a dictionary in Python

In this tutorial, we are going to see how to copy a dictionary in Python. Dictionary in Python is an unordered collection of values, which maps one set of objects(keys) to another set of objects(values) unlike other data types that contain only one value as an element.

When we assign

dictionaryA = dictionaryB
dictionaryA = dictionaryB these refers to the same dictionary. In this tutorial we will see different ways to copy a dictionary to another dictionary.
 
[st_adsense]  

How to copy a dictionary using copy() method

The copy() method returns a shallow copy of the existing dictionary.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Dictionary of languages
languages_dic = {
"Python": 55,
"Java" : 142,
"PHP" : 32,
"C" : 13,
}
# copy the languages dictionary into copy_dic
copy_dic = languages_dic.copy()
# Display the initial dictionary
print("Initial dictionary = ", languages_dic)
# Display the copy of the dictionary
print("Copy of the dictionary = ", copy_dic)
# Dictionary of languages languages_dic = { "Python": 55, "Java" : 142, "PHP" : 32, "C" : 13, } # copy the languages dictionary into copy_dic copy_dic = languages_dic.copy() # Display the initial dictionary print("Initial dictionary = ", languages_dic) # Display the copy of the dictionary print("Copy of the dictionary = ", copy_dic)
# Dictionary of languages
languages_dic = {
    "Python": 55,
    "Java" : 142,
    "PHP" : 32,
    "C" : 13,
} 

# copy the languages dictionary into copy_dic
copy_dic = languages_dic.copy() 

# Display the initial dictionary 
print("Initial dictionary = ", languages_dic) 

# Display the copy of the dictionary 
print("Copy of the dictionary = ", copy_dic)

Output:

Initial dictionary = {'Java': 142, 'C': 13, 'Python': 55, 'PHP': 32}
Copy of the dictionary = {'Java': 142, 'C': 13, 'Python': 55, 'PHP': 32}

 
[st_adsense]  

How to copy a dictionary using the dict() constructor

dict() is a constructor that creates a dictionary in Python.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Dictionary of languages
languages_dic = {
"Python": 55,
"Java" : 142,
"PHP" : 32,
"C" : 13,
}
# copy the languages dictionary into copy_dic
copy_dic = dict(languages_dic)
# Display the initial dictionary
print("Initial dictionary = ", languages_dic)
# Display the copy of the dictionary
print("Copy of the dictionary = ", copy_dic)
# Dictionary of languages languages_dic = { "Python": 55, "Java" : 142, "PHP" : 32, "C" : 13, } # copy the languages dictionary into copy_dic copy_dic = dict(languages_dic) # Display the initial dictionary print("Initial dictionary = ", languages_dic) # Display the copy of the dictionary print("Copy of the dictionary = ", copy_dic)
# Dictionary of languages
languages_dic = {
    "Python": 55,
    "Java" : 142,
    "PHP" : 32,
    "C" : 13,
} 

# copy the languages dictionary into copy_dic
copy_dic = dict(languages_dic) 

# Display the initial dictionary 
print("Initial dictionary = ", languages_dic) 

# Display the copy of the dictionary 
print("Copy of the dictionary = ", copy_dic)

Output:

Initial dictionary = {'Java': 142, 'C': 13, 'Python': 55, 'PHP': 32}
Copy of the dictionary = {'Java': 142, 'C': 13, 'Python': 55, 'PHP': 32}

 

How to copy a dictionary using dict.items() method

items()
items() returns an iterable sequence of all key-value pairs in the dictionary.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Dictionary of languages
languages_dic = {
"Python": 55,
"Java" : 142,
"PHP" : 32,
"C" : 13,
}
# copy the languages dictionary into copy_dic
copy_dic = {key:value for key, value in languages_dic.items()}
# Display the initial dictionary
print("Initial dictionary = ", languages_dic)
# Display the copy of the dictionary
print("Copy of the dictionary = ", copy_dic)
# Dictionary of languages languages_dic = { "Python": 55, "Java" : 142, "PHP" : 32, "C" : 13, } # copy the languages dictionary into copy_dic copy_dic = {key:value for key, value in languages_dic.items()} # Display the initial dictionary print("Initial dictionary = ", languages_dic) # Display the copy of the dictionary print("Copy of the dictionary = ", copy_dic)
# Dictionary of languages
languages_dic = {
    "Python": 55,
    "Java" : 142,
    "PHP" : 32,
    "C" : 13,
} 

# copy the languages dictionary into copy_dic
copy_dic = {key:value for key, value in languages_dic.items()} 

# Display the initial dictionary 
print("Initial dictionary = ", languages_dic) 

# Display the copy of the dictionary 
print("Copy of the dictionary = ", copy_dic)

Output:

Initial dictionary = {'Java': 142, 'C': 13, 'Python': 55, 'PHP': 32}
Copy of the dictionary = {'Java': 142, 'C': 13, 'Python': 55, 'PHP': 32}
[st_adsense] mcq

Leave a Reply

Your email address will not be published. Required fields are marked *