mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-11-22 21:52:31 +03:00
Better explanation of list slices
Step size and list slice syntax was not quite clear. Upon seeing li[::-1] as it was previously, I was not sure what this really meant so I looked it up and found that this section of the guide may benefit from more explanation.
This commit is contained in:
parent
5ff5f6c0d8
commit
da3b7c3520
@ -160,8 +160,12 @@ li[1:3] #=> [2, 4]
|
|||||||
li[2:] #=> [4, 3]
|
li[2:] #=> [4, 3]
|
||||||
# Omit the end
|
# Omit the end
|
||||||
li[:3] #=> [1, 2, 4]
|
li[:3] #=> [1, 2, 4]
|
||||||
|
# Select every second entry
|
||||||
|
li[::2] #=>[1,4]
|
||||||
# Revert the list
|
# Revert the list
|
||||||
li[::-1] #=> [3, 4, 2, 1]
|
li[::-1] #=> [3, 4, 2, 1]
|
||||||
|
# Use any combination of these to make advanced slices
|
||||||
|
# li[start:end:step]
|
||||||
|
|
||||||
# Remove arbitrary elements from a list with "del"
|
# Remove arbitrary elements from a list with "del"
|
||||||
del li[2] # li is now [1, 2, 3]
|
del li[2] # li is now [1, 2, 3]
|
||||||
|
Loading…
Reference in New Issue
Block a user