×

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's Qstandarditemmodel

Python More interactive and user-friendly than any other programming language is Python. Many libraries are used by the python programming language to speed up procedures. Python can be used to develop...

3 minutes read.

How to Sort a String in Python?

The characters in the string are sorted or put in alphabetical order using the sort string function in Python. Python has built-in techniques for sorting strings available. Since we occasionally...

6 minutes read.

Python slice()

Python slice() class The slice() class return a slice object representing the set of indices specified by range(start, stop, step).  Syntax class slice(stop)          or class slice(start, stop[, step]) Parameter Start: This parameter represents an integer number specifying at...

1 minute read.

Subprocess in Python

In this tutorial, we will understand what is a subprocess in python and will understand how to use it. Subprocess A subprocess is a very prevailing portion of the python library. It...

3 minutes read.

Python Parallel Processing

By performing more jobs concurrently, your software may complete more tasks in a shorter amount of time. These aid in solving major issues. The following subjects will be covered in...

2 minutes read.

Python Matrix Multiplication

One of the most fundamental mathematical structures, matrices are used often in many disciplines, including mathematics, physics, engineering, computer science, etc. For example, matrices and associated operations (multiplication and addition) are...

8 minutes read.

Paramiko Python Example

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

6 minutes read.

Python Time Library

We will consider various functions given by the python module library with examples.This python time module helps to work on time in python to get the current time.Before going with...

4 minutes read.

Python Control Flow Statements

This article aims to introduce you to what control flow statements are in general and Control Flow Statements in Python programming Language, the Importance of control flow statements and look...

3 minutes read.

Artificial intelligence mini projects with source code in Python

Project Name: Movie recommendation system A recommendation provides customers with relevant information related to their searches. Before the recommendation system, the most common method of purchasing was to rely on the...

4 minutes read.

Python Decorator

Python Decorator: A Decorator is an interesting feature of Python that helps the user design patterns and insert a new functionality to an existing object without making any modifications in...

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

Python exe() function

Python exe() function The exec() function in Python executes the specified Python code and accepts large blocks of code. It supports dynamic execution of Python code where the object must be either a string...

1 minute read.

Python List Size

Introduction The list data type in Python is an ordered, flexible collection. A list may also contain duplicate entries. To get the size of any object, use the len() function in...

6 minutes read.

Managing Multiple Python Versions With pyenv

Introduction If you had ever wished to assist to an application that makes use of several different Python versions but weren't sure whether you would quickly test them all? Do you...

4 minutes read.

Hypothesis Testing in Python

Hypothesis Testing in python is widely used along with statistics. Many libraries in Python are very useful for statistics and machine learning. Libraries like numpy, scripy etc. help in hypothesis testing in...

12 minutes read.

CSV Module In Python

Introduction CSV stands for "comma separated values". It is the most common and simple file structure used for storing and arranging tabular data. for example; a spreadsheet or database. It stores...

5 minutes read.

Python callable() Function

Python callable() Function The callable() function returns a boolean value ‘True’ if the specified object is callable, else it returns False. Syntax callable(object) Parameter Object:  The object parameter represents the value to test if it is callable...

1 minute read.

Python sort() function

Python provides many built-in functions for solving many problems that arise in different situations in programs. One of such methods is the sort () method. In this article, the syntax...

4 minutes read.

Python Parse Text File

We will learn different ways of read text records in Python. TL;DR The accompanying tells the best way to read all texts from the readme.txt document into a string: with open('readme.txt') as f: lines...

5 minutes read.