Python Forum
Which approach is better to copy a list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Which approach is better to copy a list?
#1
I was reading Python Crash Course (excellent book btw!), in it, the author copies a list by slicing it, and demonstrates that the new list is independent, any change in the new list, will not be reflected in the old one. On SO, the accepted answer states the prefer method is:

new_list = list(old_list) 


because it's more readable.

On Slicing:

Quote:it is a weird syntax and it does not make sense to use it ever. ;)

I prefer using the [:], it's simple and understandable to me. Any reason why I should stop using it?
Reply
#2
That's one guy's opinion. In my experience, [:] is pretty common. Also, the second comment on that stack overflow thread notes that [:] is much faster.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Quote:Any reason why I should stop using it?
The one time you would not want to use it is when you need a deepcopy in which [:] is a shallow copy.
Recommended Tutorials:
Reply
#4
(Oct-15-2017, 06:05 PM)metulburr Wrote:
Quote:Any reason why I should stop using it?
The one time you would not want to use it is when you need a deepcopy in which [:] is a shallow copy.

As the PCC book pointed out, if you use [:], the second list has no effect on the first list, you can add items to the second and it will not be reflected in the first. Is there more that goes on in the deep copy that I'm unaware of?
Reply
#5
You might have another list in the list:

>>> a = [1, 2, 3]
>>> b = [3, 4, 5]
>>> c = [a, b]
>>> d = c[:]
>>> d.append(1)
>>> c
[[1, 2, 3], [3, 4, 5]]
>>> d
[[1, 2, 3], [3, 4, 5], 1]
>>> d[0].append(1)
>>> a
[1, 2, 3, 1]
>>> c
[[1, 2, 3, 1], [3, 4, 5]]
So I put list a in list c. I then copy list c into list d. I can now mess with list d without affecting list c. However, the list a that is in both list c and list d is the same list a. So if I mess with the list a through list d, it messes with the list a in list c (and the original list a). A deep copy of list d would make a copy of list a before putting it into list d.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
Quote: A deep copy of list d would make a copy of list a before putting it into list d.

You mean a deep copy of list c would make a copy of list a before putting it into list d, correct?
Reply
#7
Correct. Another lesson in why you use descriptive variable names.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Advice needed on how to approach this problem... sawtooth500 1 247 Apr-06-2024, 01:55 PM
Last Post: snippsat
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 252 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy List Not Copying BAbdulBaki 3 617 Aug-19-2023, 02:03 AM
Last Post: perfringo
  About list copy. water 3 1,550 Apr-03-2022, 02:42 AM
Last Post: deanhystad
Question Making a copy list in a function RuyCab 1 1,793 Jul-11-2021, 02:06 PM
Last Post: Yoriz
  Sound Approach for Running a Shell Command? matt_the_hall 8 3,344 Dec-14-2020, 02:52 PM
Last Post: matt_the_hall
  Need feedback on my approach for python dashboard for Asana pashtett 0 1,317 Nov-24-2020, 11:51 AM
Last Post: pashtett
  Approach to creating Audit table pynewbie 4 3,832 Feb-24-2020, 06:12 PM
Last Post: pynewbie
  list approach due nested order 3Pinter 6 2,818 Oct-07-2019, 01:49 PM
Last Post: 3Pinter
  Whats a good design/approach? hshivaraj 1 1,766 Sep-16-2019, 01:44 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020