2

if I have a list of integer

    x=[0, 0, 119, 101, 108, 108, 99, 111, 109]

I'd like to cut 2 elements from left

    x=[119, 101, 108, 108, 99, 111, 109]

What shall I do ?

Wittaya
  • 389
  • 1
  • 3
  • 10

2 Answers2

6

Use Python's slice notation:

>>> x = [0, 0, 119, 101, 108, 108, 99, 111, 109]
>>> x = x[2:]
>>> x
[119, 101, 108, 108, 99, 111, 109]

This gets every element from the third item to the end of the list, and we just make x the value of that.

Community
  • 1
  • 1
TerryA
  • 58,805
  • 11
  • 114
  • 143
  • I'd suggest a brief description of in-place assignment as part of your answer: `x[:] = x[2:]`. Otherwise you're creating a copy, which IRL may not be desirable. – kojiro Sep 21 '13 at 23:04
  • 2
    @kojiro Would it matter in this case though? I don't see an issue with it :3 – TerryA Sep 21 '13 at 23:06
  • It wouldn't matter in this case, but OP and future readers may not know about it. I'm just suggesting a slight improvement to the answer. – kojiro Sep 21 '13 at 23:09
  • Even if the OP doesn't give a damn, complete answers are nice for Stack Overflow since questions are often referenced repeatedly over a long period of time. – Shashank Sep 21 '13 at 23:10
  • 1
    @kojiro I still don't see why it's an issue. Where could it come up? – TerryA Sep 21 '13 at 23:12
  • Haidro, did you forget your user's page ? _"I'm no expert when it comes to Python. If anything, I'm learning off this site more than people are by asking questions."_ Your answer isn't the best it can be, several people say it to you, listen. – eyquem Sep 21 '13 at 23:31
  • @eyquem I'm saying that this won't cause any problems at all. *Where* would there be a problem? This is what I'm trying to ask, and there I'll learn something ;) – TerryA Sep 21 '13 at 23:34
  • @Haidro Well, and now that I've posted my first edit, what do you think of POTENTIAL issue ? Not sure, I say; only potential in certain cases. But that's different from "never an issue at all". – eyquem Sep 22 '13 at 00:57
  • 1
    @Haidro It could come up if you have more than one reference to the list and you expect them to be the same. https://gist.github.com/kojiromike/6655738 – kojiro Sep 22 '13 at 01:15
0

Nein nein nein

Sorry, I don't think that Haidro's solution to be good because a reassignation is performed that isn't necessary.

See:

x = [0, 0, 119, 101, 108, 108, 99, 111, 109]
print id(x)
x = x[2:]
print id(x)

print

y = [0, 0, 119, 101, 108, 108, 99, 111, 109, 1003]
print id(y)
y[:2] = []
print id(y)

result

18713576
18739528

18711736
18711736

EDIT 1

Showing an example of x = x[2:] causing an issue:

x = [0, 0, 119, 101, 108, 108, 99, 111, 109, 1003]
print id(x),x

gen = ((i,a+100) for i,a in enumerate(x))

for u in xrange(4):
    print '%d %d' % gen.next()

x = x[2:]
print 'x = x[2:]\n',id(x),x

for u in xrange(4):
    print '%d %d' % gen.next()

print '--------------------------'

y = [0, 0, 119, 101, 108, 108, 99, 111, 109, 1003]
print id(y),y

gen = ((i,b+100) for i,b in enumerate(y))

for u in xrange(4):
    print '%d %d' % gen.next()

y[:2] = []
print 'y[:2] = []\n',id(y),y

for u in xrange(4):
    print '%d %d' % gen.next()

result

18713576 [0, 0, 119, 101, 108, 108, 99, 111, 109, 1003]
0 100
1 100
2 219
3 201
x = x[2:]
18711736 [119, 101, 108, 108, 99, 111, 109, 1003]
4 208
5 208
6 199
7 211
--------------------------
18740128 [0, 0, 119, 101, 108, 108, 99, 111, 109, 1003]
0 100
1 100
2 219
3 201
y[:2] = []
18740128 [119, 101, 108, 108, 99, 111, 109, 1003]
4 199
5 211
6 209
7 1103

In the first case ( x = x[2:] ) , x is reassigned to another object. But the generator created before the reassignement still uses the same list object to yield elements. This may be an issue in certain codes.
But it's true that the other code could also be an issue in other cases.
What I mean is that we aren't protected from x = x[2:] to cause an issue, in the absolute.

EDIT 2

z[:] = z[2:] is notably different from y[:2] = [] :

import dis

print 'y[:2] = []'
def f(z):
    y[:2] = []
dis.dis(f)
print '====================='
print 'z[:] = z[2:]'
def f(z):
    z[:] = z[2:]
dis.dis(f)

result

y[:2] = []
  7           0 BUILD_LIST               0
              3 LOAD_GLOBAL              0 (y)
              6 LOAD_CONST               1 (2)
              9 STORE_SLICE+2       
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE        
=====================
z[:] = z[2:]
 12           0 LOAD_FAST                0 (z)
              3 LOAD_CONST               1 (2)
              6 SLICE+1             
              7 LOAD_FAST                0 (z)
             10 STORE_SLICE+0       
             11 LOAD_CONST               0 (None)
             14 RETURN_VALUE  
eyquem
  • 26,771
  • 7
  • 38
  • 46
  • In the line `y[:2] = []` you very likely meant to put `y[:] = y[2:]`. In its current state your answer does not address the original question since it is merely a criticism of Haidro's answer (and therefore should be in a comment). – Shashank Sep 21 '13 at 23:31
  • @ShashankGupta I don't know what you mean by _"likely meant"_. What I see is that the disassemblies of these two instructions are different, see my second edit. By the way, are you the one who downvoted my answer ? – eyquem Sep 22 '13 at 00:20
  • yes I did downvote your answer since it didn't even address the question. You just made this post to criticize Haidro's answer which I find a bit silly. When I said "likely meant" I was referring to the fact that you didn't seem to be talking to the OP at all and I felt that you should have left this "answer" as a comment instead. – Shashank Sep 22 '13 at 00:30
  • @ShashankGupta I cant' imagine how you can say that I don't address the question. In the question the OP says **I'd like to cut 2 elements from left** : that's PRECISELY what my code does. – eyquem Sep 22 '13 at 00:45
  • I would upvote your answer if you began with an answer to the question such as: `x[:2] = []` followed by an explaination of why this form is the best. – dansalmo Sep 22 '13 at 00:45
  • @ShashankGupta You wrote _"you didn't seem to be talking to the OP at all "_ You are wrong. I talked to OP as well as to everybody else. Indeed, i didn't talk to only the OP, I talked to everybody who had read this thread and upvoted the Haidro's answer and everybody who is likely to read this thread in the future as **kojiro** suggested in a comment and as I thought you had yourself done when writing _"questions are often referenced repeatedly over a long period of time"_ – eyquem Sep 22 '13 at 00:53
  • I get what your code does. I just think answers to questions shouldn't start off with "X's answer is wrong" followed by a long explanation as to why someone else's answer is wrong. Answers should be succinct and directed towards the OP to keep this site clean. And anyways my downvote was just my opinion. Feel free to take it how you will. – Shashank Sep 22 '13 at 00:59
  • @dansalmo I wouldn't pretend that my solution is the BEST in all situations. There may be cases where the effect of reassignement would be a desired effect. But presently, we read in the question: **I'd like to CUT 2 elements from left** – eyquem Sep 22 '13 at 00:59
  • @ShashankGupta It seems that a lot of people draw a great pleasure from maintaining all clean and to make the rules respected. Personnally I would find very sad and depressing if SO was just a site of Q and A as clean as a marble surface in which no debate and contradictions were authorized, with humans trying to mimic in their writings the neutral style of a handbook. – eyquem Sep 22 '13 at 01:17