Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Python FOR LOOPS

Name: Anonymous 2008-04-23 15:44

What the fuck is with Python's for loops.

What's the difference between:

for i in list[0]:

for i in list[0:]:

for i in list[:0]:

Name: Anonymous 2008-04-23 15:45

:0]

Name: Anonymous 2008-04-23 15:52

>>2
Holy fuck I lol'd harder than I lol at }; every time.

Name: Anonymous 2008-04-23 15:52

>>1
The first one runs the for loop once, with i set to list[0], the second one runs the loop for each element of list, and the third one does nothing.

To see why, enter list[0], list[0:] and list[:0] in the interpreter.

Name: Anonymous 2008-04-23 15:54

for i in list[0]: every item in list[0]

for i in list[0:]: every item in list from list[0] to the end

for i in list[:0]: every item in list from the start to list[0]

Name: Anonymous 2008-04-23 16:40

>>4
The First one wIll probably raise a TypeError, depending on whether or nOt the first element of "list" is iterable.
The seCond one will iterate through every element of list.
The third one will skip the body of the loop since list[:0] is empty.

Name: Anonymous 2008-04-23 16:47

>>7

I see what you did there.

Name: Anonymous 2008-04-23 18:25

>>8

I see what you did there.

Name: Anonymous 2008-04-23 18:33

>>7-9
Stop that.

Name: Anonymous 2008-04-23 18:38

hax my anus?

Name: Anonymous 2008-04-23 19:39

>>6
Oh, good point. Didn't think about the type error on the first.

Name: Anonymous 2008-04-23 20:28

>>15
DAMN YOU.

Name: Anonymous 2008-04-23 21:55

>>12
wut

Name: Anonymous 2008-04-24 0:11

This isn't even an issue with for loops; you're confused with python's slicing.

It's a little bit terse, yeah.  I get confused too, especially because of the following:

In [8]: list=[1,2,3,4]
In [9]: list[1:3]
Out[9]: [2, 3]           ## sequential splices are non-inclusive on the second argument, and for no obvious reason.

Name: Anonymous 2008-04-24 0:15

>>12
FUCK YOU.

Name: Anonymous 2008-04-24 0:34

>>12,15
SPAWHBTC

Name: Anonymous 2008-04-24 1:09

Name: Anonymous 2008-04-24 5:27

>>17
And the funny thing is, he got the same quality of answers in both boards, the only difference being that /prog/ added some chucklesome nonsense to the thread also.

Name: Anonymous 2008-04-24 5:48

>>18
YEAH /prog/ IS SUPERIOR WE ARE BETTER

Name: Anonymous 2011-01-31 19:58

<-- check em dubz

Name: Anonymous 2011-02-04 16:33

Name: Sgt.Kabukiman䖉 2012-05-22 23:18

All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List