×

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

Python List count() method

Python List count() method The list.count() method returns the number of times x appears in the list. Syntax list.count(x) Parameter x: This parameter represents the value to search for and can contain any iterable (list, set, tuple, etc.) Return This method returns the...

1 minute read.

Python Set remove() method

Python Set remove() method The set.remove() method in Python removes the specified element from the set if it is present in the set else this method will raise an error if the specified item does...

2 minutes read.

How to set up a proxy using selenium in python

What is Proxy? A proxy acts as a middleman between user requests and server responses. The main purposes of proxies are to protect user privacy and encapsulate data between various interactive...

3 minutes read.

Python random.seed() function

The random module in Python produces a random number or pseudo-random data, that is, deterministic. The seed function records the state of a random function to provide the same random...

6 minutes read.

Python Assert

Python Assert Python provides an assert statement which is used to check the logical expression. If the given logical expression is true, then it precedes for the next line; otherwise, it raises an...

2 minutes read.

Python | a += b is not always a = a + b

a += b in Python doesn't always behave the same way as a = a + b; the same operands can produce different outcomes depending on the circumstances. But we...

3 minutes read.

Difference between Sort and Sorted in Python

If you new to Python, it must be confusing the distinction between the Sort and Sorted functions. However, it is important to understand the differences in order to use them...

5 minutes read.

Python Modulo

For basic calculations, Python provides operators. Python supports a broad range of Arithmetic Operators to do arithmetic, as given below: +Addition*Multiplication-Subtraction/Division//Floor division**Exponentiation%Remainder/Modulus As you can see, one of these basic arithmetic operators...

8 minutes read.

Python Linear regression

In this tutorial, we will understand the meaning, usage, and types of Linear Regression. Further, we will comprehend the terms cost function and Optimization. Linear Regression is the widely used Machine...

3 minutes read.

Is Python Case-sensitive when Dealing with Identifiers

Yes, Python is a case-sensitive language while dealing with identifiers. Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. It is a case-sensitive...

6 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.

GUI Calendar using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

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.

Python String splitlines() method

Python String splitlines() method The string.splitlines() method in Python splits the specified string and returns a list of the lines in the string, breaking at line boundaries.  Syntax splitlines([keepends]) Parameter keepends(optional): This parameter specifies if...

1 minute read.

Python TCP Server

The TCP server library is also known as the socket library. Socket programming is the method of connecting two nodes through a network to form communication between two nodes. In...

3 minutes read.

Python IDE names

Python is one of the most renowned programming languages. It has different execution conditions and a great many compilers to execute the python programs, e.g., PyCharm, PyDev, Jupyter Notebook, Visual...

6 minutes read.

How to Add Elements in List in Python?

How to Add Elements in List in Python We all are aware of the wide spectrum of applications where the data structures of Python are used. Be it lists, tuples, or...

4 minutes read.

CatPlot in Python

Python Seaborn Library Seaborn is a superb Python tool for displaying graphical statistics graphing. Seaborn provides different color schemes and attractive default styles to facilitate the creation of various statistics charts...

8 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.

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.