tags:

views:

84

answers:

2

Hello All:

I'm new to Python. I see : used in list index especially when it's associated with functional calls, for e.g., python 2.7 document suggests that lists.append translates to a[len(a):] = [x]. Why do one have to suffix len(a) with a colon?

I understand that : is used to identify keys in dictionary. Thanks in advance for the help.

+2  A: 

: is the delimiter of the slice syntax to 'slice out' sub-parts in sequences , [start:end]

[1:5] is equivalent to "from 1 to 5" (5 not included)
[1:] is equivalent to "1 to end"
[len(a):] is equivalent to "from length of a to end"

Watch http://www.youtube.com/watch?v=tKTZoB2Vjuk at around 40:00 he starts explaining that.

Works with tuples, dictionaries and lists, too.

Soulseekah
Yeah, I guess downvoting is what people helping people who can't help themselves by using Google and the Python documentation deserve.
Soulseekah
Remember that [1:5] starts with the object at index 1, and the object at index 5 is not included. You can also make a soft copy of a list with [:]
Garrett Hyde
+1 thanks for pointing it out, forgot about that.
Soulseekah
+4  A: 

slicing operator. http://docs.python.org/tutorial/introduction.html#strings and scroll down a bit

joni
He should probably skip the "scroll down" part and just read the whole thing.
Glenn Maynard