×

Python Dictionary copy() method

Python Dictionary copy() method

The dictionary.copy () method in Python returns a copy of the specified dictionary.

Syntax

dictionary.copy ()

Parameter

NA

Return

None

Example 1

 # Python program explaining
 # the dictionary.copy() method
 # initialising the dictionary
 fruits = {
  "banana": "apple",
   "orange": "mango",
   "grapes": 5
 }
 # printing the dictionary
 print("Dictionary:",fruits)
 # copying the dictionary elements.
 dictionary2= fruits.copy()
 print("After the copying the dictionary...")
 print("Dictionary2:",fruits) 

Output

 Dictionary: {'grapes': 5, 'orange': 'mango', 'banana': 'apple'}
 After the copying the dictionary...
 Dictionary2: {'grapes': 5, 'orange': 'mango', 'banana': 'apple'} 

Example 2

 # Python program explaining
 # the dictionary.copy() method
 #initializing the dictionary
 dictionary = {1: "numbers", 2: "digits"}
 # copying the elements of dictionary in dictionary1
 dictionary1 = dictionary.copy()
 print('dictionary: ', dictionary)
 print("After copying the dictionary elements in dictionary1...")
 print('dictionary1: ', dictionary1) 

Output

 dictionary:  {1: 'numbers', 2: 'digits'}
 After copying the dictionary elements in dictionary1...
 dictionary1:  {1: 'numbers', 2: 'digits'} 

Example 3

 # Python program explaining
 # the dictionary.copy() method
 dictionary = {1:'one', 2:'two',3:'three'} 
 # copying the original dictionary using copy() function 
 copyElement = dictionary.copy() 
 # removing all elements from the dictionary 
 # Only new dictionary becomes empty as copy() 
 # does shallow copy. 
 copyElement.clear() 
 # printing the dictionary after clearing all elements  
 print('new: ', copyElement) 
 # printing the original elements
 print('original: ', dictionary) 

Output

new:  {}
original:  {1: 'one', 2: 'two', 3: 'three'}


Related Topics

Scraping data in python

Data scraping is a technique in which one program extracts a set of data from the output of another program. Web scraping is the most common application of this technique....

6 minutes read.

Python File Handling

What is the file? The file is a collection of data in a single unit. It is used to store information in the non-volatile memory such as hard-disk. Computer programs generally run on the RAM...

7 minutes read.

Find whether the given stringnumber is palindrome or not

Problem statement Sam is found of playing with strings. One day he thought of finding whether a string is a palindrome or not. He wanted to develop a computer process to...

2 minutes read.

Import Function in Python

In Python programming language, the import keyword plays an important role in the whole code because it makes one module make available in another module. Import is used to structure...

3 minutes read.

Python Tuple Methods

Python Tuple Methods Python has two built-in methods that are used for tuples. The following are the two methods: Method Description count() The tuple.count() method in Python returns the number of times a...

1 minute read.

tell() function in Python

Introduction The tell() function in Python is used to return the current position of the file read/write pointer within the file. An integer value is returned by this method, and it...

2 minutes read.

Python try catch exception

The try-except proclamation can deal with exceptions. Exceptions might happen when you run a program. Exceptions are blunders that occur during the execution of the program. Python won't educate you regarding...

3 minutes read.

Python MySQL Client

MySQL client is a project that is the subdivision of the MySQLdb package. This package provides an interface that runs the Python database API. Installing mysqlclient You can easily install mysqlclient with...

5 minutes read.

wxPython Panel class

wxPython Panel class The Widgets which is shown in the frame of GUI window such as text box, buttons, static text etc. are put inside the panel class of the wxpython...

2 minutes read.

How to create a class in python

In any programming language, a class is a user-defined plan or blueprint using which objects or instances of the class are created. You may wonder why we need classes in programming....

5 minutes read.

Python String rstrip() method

Python String rstrip() method The string.rstrip() method in Python returns a copy of the string with trailing characters removed. Syntax string.rstrip([chars]) Parameter chars:  This argument represents a string specifying the set of characters to be...

1 minute read.

Standard Scalar in Python

Python is a vast and open-source language that contains many libraries, modules, and functions. These functions in python are reusable codes where we don’t need to type the whole code...

4 minutes read.

Python SymPy

A Python package for symbolic mathematics is called SymPy. Its goal is to develop into a fully-fledged computer algebra system (CAS) while maintaining the code as straightforward as possible to...

3 minutes read.

Iterate a Dictionary in Python – Part 2

In this tutorial, we will learn above various methods used to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to...

3 minutes read.

Python exit commands

exit(), quit(), sys.exit(), os._exit() In this tutorial, we will study exit commands used in the Python programming language. Python is undoubtedly the choice of programmer and this is because of the in-built...

3 minutes read.

Python Prime factorization

Python Prime factorization In this tutorial, we will design a program where we will find all the prime factors of a number. Then, we will print all these prime factors of...

3 minutes read.

Python for Data Analysis

Data analysis uses various techniques to read, illustrate, manipulate and evaluate a particular data. You can have access to the data and keep the data updated regularly. You can append...

4 minutes read.

Check if a String is Empty in Python

The string is one of the data types of Python. This data type stores all kinds of data but all the characters must be enclosed using double quotes (""). In...

5 minutes read.

How to import numy in python

How to Install NumPy in Python Python is a vast ocean of libraries, modules, and different functions. It has a solution for almost everything. Using Python, we can simplify even a...

3 minutes read.

Python Fit Transform

In machine learning, fit (), transform(), fit transform () methods are provided by scikit learn package. This package is used in model fitting and data processing. These methods are implemented...

2 minutes read.