BoomChuck
0
Q:

slicing in python list

word = "Example"

# Obtain the first 3 characters of "Example"
# E x a m p l e
# 0 1 2 3 4 5 6
# First 3 characters = "Exa"
sliced_word = word[:3] # Gives you "Exa"

# Everything after character #3 = "mple"
second_sliced_word = word[3:] # Gives you "mple"
4
a = [1,2,3,4,5]
a[m:n] # elements grrater than equal to m and less than n
a[1:3] = [2,3]
3
# [start:stop:step]
# for example ...
l = [1, 2, 3, 4, 5]
l[1:4] # elements from 1 (inclusive) to 4 (exclusive)
l[2:5:2] # elements from 2 (inclusive) to 5 (exclusive) going up by 2
2
sample_list = [1,2,3,4,5,6]
new_list = sample[1:4]
# index slicing using index
# print(new_list) = [2,3,4,5]
1

New to Communities?

Join the community