-2

It might be that I don't understand slicing that well. I want to add one list from a particular index to another list. I was wondering, because I read that if you slice a list that you make a (deep)copy of it. Which method below is faster?

method 1

new_lst += old_lst[i:]

method 2

while i < len(old_lst):
    new_list.append(old_lst[i])
    i += 1
martineau
  • 119,623
  • 25
  • 170
  • 301
Caspertijmen1
  • 351
  • 1
  • 14
  • 7
    Slice is a shallow copy, not a deep copy. – Barmar Feb 27 '21 at 00:16
  • 3
    [Premature optimization is the root of all evil](http://c2.com/cgi/wiki?PrematureOptimization). Do whatever you feel is most natural and readable, and optimize it if it becomes a performance bottleneck. – Barmar Feb 27 '21 at 00:19
  • First method is faster than using hand-written `for` loop. – martineau Feb 27 '21 at 00:20
  • 3
    Probably the first, but why didn't you just time it and see? – Tomerikoo Feb 27 '21 at 00:21
  • Append is a slow process. However, both methods create a shallow copy: see [this post](https://stackoverflow.com/questions/17873384/how-to-deep-copy-a-list) on how to create a deep copy of a list. – Kraigolas Feb 27 '21 at 00:23
  • 1
    I would think you want to use extend instead, like `new_lst.extend(old_lst)`. – Bobby Ocean Feb 27 '21 at 00:30
  • the while loop will call `append` too many times which produces function overhead. `[i:]` will make a copy of your array in 1 call. – thethiny Feb 27 '21 at 00:36
  • There are multiple related posts you can read first: [How to deep copy a list?](https://stackoverflow.com/q/17873384/2745495) to address the misconception on deepcopying a list, and [How do I concatenate two lists in Python?](https://stackoverflow.com/q/1720421/2745495) to see other ways to extend new_list with contents from old_list. And as mentioned, you can always time it yourself to see which is faster. – Gino Mempin Feb 27 '21 at 02:36

1 Answers1

1

The while loop will call append multiple times which produces function overhead. [:] (shallow copy) on the other hand will make a copy of your array in one call. However if your elements are not literals (if they're a list for example) then you will end up accidentally sharing the pointers between lists. If you really want to deep copy, then utilize from copy import deepcopy which will deep copy your iterable.

thethiny
  • 1,125
  • 1
  • 10
  • 26
  • Both methods make shallow copies, so they have the same sharing behavior for the list elements. – Barmar Mar 01 '21 at 20:59
  • @Barmar I said that in my post. `However if your elements are not literalrs then you wil ... share pointers.` and `utilize ... deepcopy will will deep copy` – thethiny Mar 02 '21 at 21:09
  • I misunderstood what you were referring to when you said "on the other hand". You were distinguishing the number of calls, not whether it's a shallow copy. – Barmar Mar 02 '21 at 21:11
  • @Barmar that's correct. Though If I were wrong, I appreciate your attempt to correct me. Maybe someone will misunderstand like you and this chat would help him understand. – thethiny Mar 03 '21 at 22:06