3

I am a newbie to python,everywhere I read about list methods I see one thing

The slice method returns a "new" list

What is here meant by "new" list,and why is it faster then changing the original list?

Does it really matter if python manipulates the original list,I mean I cant use it anyway.

Kartik Anand
  • 4,513
  • 5
  • 41
  • 72
  • 1
    by the way, if you want to check if you still have the same object in python you could always compare before and after with [id()](http://docs.python.org/library/functions.html#id) – snies May 16 '12 at 14:51

4 Answers4

3

With lists, you can do both:

1) create a new list (the original is left intact):

In [1]: l = [1, 2, 3, 4, 5]

In [2]: l[:3]
Out[2]: [1, 2, 3]

In [3]: l
Out[3]: [1, 2, 3, 4, 5]

2) modify the list in-place:

In [6]: del l[3:]

In [7]: l
Out[7]: [1, 2, 3]

In [8]: l.append(15)

In [9]: l
Out[9]: [1, 2, 3, 15]

It's up to you to choose which way makes more sense for your problem.

In contrast to lists, tuples are immutable, which means that you can slice them, but you cannot modify them in place.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • so when I use append method what happens then??the original list is manipulated or a new one is created and now my variable name points to the new one? – Kartik Anand May 16 '12 at 14:51
  • @KartikAnand: `append()` modifies the list in-place. – NPE May 16 '12 at 14:52
  • so the append function wouldnt return anything..am i right here? – Kartik Anand May 16 '12 at 14:54
  • @KartikAnand: Yes: http://docs.python.org/tutorial/datastructures.html#more-on-lists – NPE May 16 '12 at 14:55
  • 2
    @KartikAnand: It's for questions like this that documentation was created. Just look it up. – Steven Rumbalski May 16 '12 at 14:55
  • see these: http://docs.python.org/tutorial/introduction.html#lists http://docs.python.org/tutorial/datastructures.html, one is an informal intro to lists, the other the reference. – Levon May 16 '12 at 14:56
  • sorry I guess...I thought "dive into python" would be enough..but i guess i'll have to read the doc carefully now..thanks.. – Kartik Anand May 16 '12 at 14:57
  • I find that if you have a question like 'so the append function wouldnt return anything' it is very educative to try it out and see for yourself. – Niek de Klein May 17 '12 at 09:23
2

I hope that this helps explain what it means by making a new list:

>>> lista = [1, 2, 3, 4]         
>>> listb = lista
>>> print lista
[1, 2, 3, 4]
>>> print listb
[1, 2, 3, 4]
>>> lista[0] = 3
>>> print listb
[3, 2, 3, 4]
>>> listc = lista[:]
>>> print listc
[3, 2, 3, 4]
>>> lista[0] = 1
>>> print listc
[3, 2, 3, 4]

When doing listb = lista you are not making a new list, you are making an additional reference to the same list. This is shown by changing the first element in lista with lista[0] = 3, this also changes the first element in listb. However, when slicing lista into listc with listc = lista[:] you are copying over the values. When changing the first element of lista back to 1 with lista[0] = 1, the first element of listc is still 3.

For speed I would expect slicing to be slower but this should not be a consideration for which one to use. As I've shown they both have a very different implication and it depends on what you are going to do with the list, rather than on speed (this is in general. There are occasion where the speed might be important).

Niek de Klein
  • 8,524
  • 20
  • 72
  • 143
0
  1. "new" means a shallow copy of the portion of the list you sliced.

  2. It depends on what you are trying to do. For your particular implementation, you might not care about the original data, but I'm sure you could come up with scenarios where you want to work with a subset of data without modifying the originals (though do keep in mind it is only a shallow copy, so there are many instances where you will be modifying the data in the original and slice when working on a slice). Also, it's not faster; in fact, it's actually slower, since the system needs to allocate memory and construct new objects. The gain is not speed, but functionality.

Silas Ray
  • 25,682
  • 5
  • 48
  • 63
0

When a function/method create a new list, it means that your script has to consume the double amount of memory and has a little (or not so little) overhead while creating a duplicate of the old list.

If the list is really big, the performance of your script can drop very fast. That's why changing lists in-place is preferred when you have large amounts of data.

KurzedMetal
  • 12,540
  • 6
  • 39
  • 65