×

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

chr() and ord() Functions in Python

Python language contains many built-in functions which we use for many programs to work efficiently. The chr() and ord() are a few built-in functions in Python that are used for...

4 minutes read.

AX Contour in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python Stack

Python Stack: The work Stack is defined as arranging a pile of objects or items on top of another. It is the same method of allocating memory in the stack...

10 minutes read.

Python Time Module

Python contains many files that can be imported into a python code and used whenever we want. One of that modules is the time module. It is a good practice...

6 minutes read.

Python any() Function

Python any() Function The any() function in Python returns a boolean value ‘True’ if any element of the iterable is true or if the iterable is empty, else it returns False. Syntax any(iterable) Parameter Iterable: An iterable object (list, tuple, dictionary) Return This...

1 minute read.

How to Install Pandas in Python?

Pandas is a library created in Python for handling and analyzing data. Pandas provides a variety of procedures and data structures for manipulating time series and numerical data.  Pandas is...

3 minutes read.

Struct Module in Python

What is a structure in Python? An entity that holds data in form of a structure is called a data structure. In Python, the data structure includes tuples, lists, dictionaries, and...

4 minutes read.

How to Practice Python Programming

Learning python kkis a step towards coding. Python gets one closer to programming languages. It is essential to practice every programming language to become a professional in coding. It is...

4 minutes read.

Python bin() function

Python bin() function The bin() function in Python returns the binary version for the specified integer. Syntax bin(x) Parameter n: This parameter represents an integer or int value. Return This function returns a binary string for the specified integer...

1 minute read.

Python Loop through a Dictionary

Introduction in this tutorial, we will discuss in python How to Loop Through a Dictionary. In contrast to other Data Types, which can only retain a single value as an element, a Dictionary...

4 minutes read.

Closest Pair of Points in Python

We are given an array of n points in the plane, and our task is to find the pair of points in the array that are the closest to each...

3 minutes read.

Front end in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The...

4 minutes read.

Python Combination

Python Combination Permutations and combinations are the important part of our mathematical tools. We use them often in our Python programs. In this tutorial, we will learn about combinations in Python...

6 minutes read.

Python Simple Interest

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

3 minutes read.

Reverse a sentence In Python

Introduction The built-in reverse() function is not supported by the Python string library. As we know, a Python string is defined as a set of Unicode characters and Python provides various...

4 minutes read.

Python Memory Management

In order to manage memory, Python uses a private heap that contains all of its data structures and objects. The Python memory manager is responsible for the internal management of...

11 minutes read.

Python tokens and character set

In this tutorial, we will understand what are character sets used in python and what is meant by python tokens and we will further discuss the type of tokens being...

4 minutes read.

Not supported between instances of str and int in python

In this article, you are going to learn why the type error occurs between instances of string and integer datatypes. Also, you will know what kind of error is the...

6 minutes read.

Time. Sleep() in Python

Python time sleep () function suspends execution for a certain seconds given by user. Time sleep () syntax: Sleep(seconds) Limitations: The number of seconds to be suspends the code as per its requirements. Returns: Void The execution...

3 minutes read.

Python dir() function

Python dir() function The dir() function in Python returns all properties and methods of the specified object, without the values. Syntax dir([object]) Parameter object: This parameter represents the object one wants to see the valid attributes. Return This function...

2 minutes read.