Skip to main content

Shallow Copy and Deep Copy in Python पायथन में गहरी प्रतिलिपि और उथली प्रतिलिपि

In Python, the concepts of deep copy and shallow copy refer to how data structures, like lists, are duplicated.

पायथन में, गहरी प्रतिलिपि और उथली प्रतिलिपि की अवधारणाएं संदर्भित करती हैं कि कैसे डेटा संरचनाएं, जैसे सूचियां डुप्लिकेट की जाती हैं।

1.) Shallow Copy उथली प्रतिलिपि :-  A shallow copy in Python creates a new object, but instead of creating copies of nested objects, it copies references to those objects. This means that if the original object is modified, changes to nested objects will reflect in both the original and the shallow copy.
पायथन में एक उथली प्रतिलिपि एक नई वस्तु बनाती है, लेकिन नेस्टेड वस्तुओं की प्रतियां बनाने के बजाय, यह उन वस्तुओं के संदर्भों की प्रतिलिपि बनाती है। इसका मतलब यह है कि यदि मूल वस्तु को संशोधित किया जाता है, तो नेस्टेड वस्तुओं में परिवर्तन मूल और उथली प्रतिलिपि दोनों में दिखाई देंगे।

You can create a shallow copy of a list using Slicing, copy method and copy module:-
आप स्लाइसिंग, कॉपी विधि और कॉपी मॉड्यूल का उपयोग करके किसी सूची की एक उथली प्रतिलिपि बना सकते हैं:-

original_list = [1, 2, [3, 4]]
shallow_copied_list = original_list[:]

original_list = [1, 2, [3, 4]]
shallow_copied_list = original_list.copy()

import copy
shallow_copied_list = copy.copy(original_list)

Example उदाहरण:-

import copy

# Original list with a nested list
original_list = [1, 2, [3, 4]]

# Creating a shallow copy
shallow_copied_list = copy.copy(original_list)

# Modifying a nested object in the original list
original_list[2][0] = 'changed'

print("Original List:", original_list)        # Output: [1, 2, ['changed', 4]]
print("Shallow Copied List:", shallow_copied_list)  # Output: [1, 2, ['changed', 4]]

# Modifying the outer object in the shallow copy
shallow_copied_list[1] = 'new_value'

print("After modifying shallow copy:")
print("Original List:", original_list)        # Output: [1, 2, ['changed', 4]]
print("Shallow Copied List:", shallow_copied_list)  # Output: [1, 'new_value', ['changed', 4]]


2.) Deep Copy गहरी प्रतिलिपि:- A deep copy in python creates a new object and recursively copies all objects found within the original. This means that changes to nested objects in the original will not affect the deep copy. 
पायथन में एक गहरी प्रतिलिपि एक नया ऑब्जेक्ट बनाती है और मूल के भीतर पाए गए सभी ऑब्जेक्ट को पुनरावर्ती रूप से कॉपी करती है। इसका मतलब यह है कि मूल में नेस्टेड ऑब्जेक्ट में परिवर्तन से डीप कॉपी पर कोई प्रभाव नहीं पड़ेगा। 

We can create a deep copy using the `copy` module:-
हम 'कॉपी' मॉड्यूल का उपयोग करके एक गहरी प्रतिलिपि बना सकते हैं:-

import copy
deep_copied_list = copy.deepcopy(original_list)

Example उदाहरण:-

import copy

# Original list with a nested list
original_list = [1, 2, [3, 4]]

# Creating a deep copy
deep_copied_list = copy.deepcopy(original_list)

# Modifying a nested object in the original list
original_list[2][0] = 'changed'

print("Original List:", original_list)        # Output: [1, 2, ['changed', 4]]
print("Deep Copied List:", deep_copied_list)  # Output: [1, 2, [3, 4]]

# Modifying the outer object in the deep copy
deep_copied_list[1] = 'new_value'

print("After modifying deep copy:")
print("Original List:", original_list)        # Output: [1, 2, ['changed', 4]]
print("Deep Copied List:", deep_copied_list)  # Output: [1, 'new_value', [3, 4]]

Comments

Popular posts from this blog

Programming with Python Language Tutorial available in hindi and english पाइथन प्रोग्रामिंग टुटोरिअल हिंदी एवं अंग्रेजी भाषा में

 Unit -01 1. Introduction to python (पाइथन भाषा का परिचय) 2. History of Python Programming Language पाइथन प्रोग्रामिंग लैंग्वेज का इतिहास 3. Python Programming Language’s Features and Advantages पायथन प्रोग्रामिंग लैंग्वेज की प्रमुख विशेषताएं 4. Python’s Programming Language's Popularity पाइथन प्रोग्रामिंग लैंग्वेज की लोकप्रियता 5.The Future of Python Programming Language पायथन प्रोग्रामिंग लैंग्वेज का भविष्य 6. The Python Interpreter पायथन इंटरप्रेटर 7. The Python IDLE पाइथन आईडीएलई 8. Installation process of Python IDLE पायथन आईडीएलई की इंस्टालेशन प्रक्रिया 9. Dynamically typed and strongly typed features of python गतिशील रूप से टाइप और दृढ़ता से टाइप की गई भाषा पायथन 10. Basic Data Types of Python पायथन के बेसिक डेटा टाइप 11. Variables in python पायथन में वेरिएबल 12. Expressions in Python पायथन में अभिव्यक्तियां 13. Statements in Python पायथन में कथन 14. Operator's in Python पायथन में ऑपरेटर्स 15. Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह 16...

Filter function of python

filter function:-  Imagine we have a bunch of things, like a list of numbers, a collection of fruits, or any other group of items. Now, let's say we want to pick out only certain items from that group based on a specific condition. This is where the filter function comes in handy. Let's say we have a list of numbers, and we want to filter out only the even numbers. We define a condition-checking function that checks if a number is even. Then, we pass this function and the list of numbers to the filter function. It goes through each number, applies the condition-checking function, and keeps only the numbers that are even. Finally, it gives us a new list containing only the even numbers. def is_even(num):     return num % 2 == 0 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] filtered_numbers = list(filter(is_even, numbers)) print(filtered_numbers) Output [2, 4, 6, 8, 10] We can also utilize lambda expressions to define filtering conditions directly within the filter function. nu...

Python Programming Language’s Features and Advantages पायथन प्रोग्रामिंग लैंग्वेज की प्रमुख विशेषताएं

Python is a versatile and popular programming language known for its simplicity and readability. It has a wide range of features that make it suitable for various types of projects. Here are some key features of the Python programming language:- पायथन एक बहुमुखी और लोकप्रिय प्रोग्रामिंग भाषा है जो अपनी सरलता और पठनीयता के लिए जानी जाती है। इसमें कई प्रकार की विशेषताएं हैं जो इसे विभिन्न प्रकार की परियोजनाओं के लिए उपयुक्त बनाती हैं। पायथन प्रोग्रामिंग भाषा की कुछ प्रमुख विशेषताएं निम्न हैं:- 1. Open Source ओपन सोर्स:-   Python is an open-source language, which encourages collaboration and allows developers to contribute to its ongoing development. पायथन एक ओपन-सोर्स भाषा है, जो सहयोग को प्रोत्साहित करती है और डेवलपर्स को इसके चल रहे विकास में योगदान करने की अनुमति देती है। 2. Integrated Development & Learning Environments एकीकृत विकास एवं अध्ययन वातावरण  (IDLEs/IDEs):-  Python has several powerful IDLEs/IDEs, such as PyCharm, VSCode, and Jupyter, that provide too...