How to clone or copy a list?

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

What are the options to clone or copy a list in Python?

While using new_list = my_list, any modifications to new_list changes my_list everytime. Why is this?

profilepic.png
manpreet 2 years ago

With new_list = my_list, you don't actually have two lists. The assignment just copies the reference to the list, not the actual list, so both new_list and my_list refer to the same list after the assignment.

To af="https://forum.tuteehub.com/tag/c">ctually f="https://forum.tuteehub.com/tag/c">copy the list, you have various possibilities:

  • You can use the builtin list.f="https://forum.tuteehub.com/tag/c">copy() method (available since python 3.3):

    new_list = old_list.f="https://forum.tuteehub.com/tag/c">copy()
  • You f="https://forum.tuteehub.com/tag/c">can slif="https://forum.tuteehub.com/tag/c">ce it:

    new_list = old_list[:]

    Alex Martelli's opinion (at least back in 2007) about this is, that it is a weird syntax and it does not make sense to use it ever. ;) (In his opinion, the next one is more readable).

  • You can use the built in list() function:

    new_list = list(old_list)
  • You can use generic f="https://forum.tuteehub.com/tag/c">copy.f="https://forum.tuteehub.com/tag/c">copy():

    import f="https://forum.tuteehub.com/tag/c">copy
    new_list = f="https://forum.tuteehub.com/tag/c">copy.f="https://forum.tuteehub.com/tag/c">copy(old_list)

    This is a little slower than list() because it has to find out the datatype of old_list first.

  • If the list contains objects and you want to copy them as well, use generic f="https://forum.tuteehub.com/tag/c">copy.deepf="https://forum.tuteehub.com/tag/c">copy():

    import f="https://forum.tuteehub.com/tag/c">copy
    new_list = f="https://forum.tuteehub.com/tag/c">copy.deepf="https://forum.tuteehub.com/tag/c">copy(old_list)

    Obviously the slowest and most memory-needing method, but sometimes unavoidable.

Example:

import f="https://forum.tuteehub.com/tag/c">copy

f="https://forum.tuteehub.com/tag/c">class Foo(objef="https://forum.tuteehub.com/tag/c">ct):
    def __init__(self, val):
         self.val = val

    def __repr__(self):
        return str(self.val)

foo = Foo(1)

a = ['foo', foo]
b = a.f="https://forum.tuteehub.com/tag/c">copy()
f="https://forum.tuteehub.com/tag/c">c = a[:]
d = list(a)
e = f="https://forum.tuteehub.com/tag/c">copy.f="https://forum.tuteehub.com/tag/c">copy(a)
f = f="https://forum.tuteehub.com/tag/c">copy.deepf="https://forum.tuteehub.com/tag/c">copy(a)

# edit orignal list and instanf="https://forum.tuteehub.com/tag/c">ce 
a.append('baz')
foo.val = 5

print('original: %r\n list.f="https://forum.tuteehub.com/tag/c">copy(): %r\n slif="https://forum.tuteehub.com/tag/c">ce: %r\n list(): %r\n f="https://forum.tuteehub.com/tag/c">copy: %r\n deepf="https://forum.tuteehub.com/tag/c">copy: %r'
      % (a, b, f="https://forum.tuteehub.com/tag/c">c, d, e, f))

Result:

original: ['foo', 5, 'baz']
list.f="https://forum.tuteehub.com/tag/c">copy(): ['foo', 5]
slif="https://forum.tuteehub.com/tag/c">ce: ['foo', 5]
list(): [
                                                    
                                                    
0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.