Colon (:) in Python list index

I am new to Python. I see : used in list indexes, especially when it is associated with function calls.

The Python 2.7 documentation assumes that lists.append translates to a[len(a):] = [x] . Why do I need the suffix len(a) with a colon?

I understand that : used to identify keys in a dictionary.

+56
python
Oct 25 '10 at 6:42
source share
3 answers

: is a slice syntax delimiter to β€œslice” parts in a sequence, [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" 

See http://www.youtube.com/watch?v=tKTZoB2Vjuk at about 40:00, he begins to explain it.

Works with tuples, dictionaries, and lists too.

+87
Oct 25 '10 at 6:53
source share
β€” -

cutting operator. http://docs.python.org/tutorial/introduction.html#strings and scroll a little

+18
Oct 25 '10 at 6:55
source share

a[len(a):] - This gives you the length to the end. He selects a range. If you cancel a[:len(a)] , this will give you the beginning of everything that len(a) .

+7
Feb 24 '14 at 23:00
source share



All Articles