xxxxxxxxxx
1
2
3
4
5
6
7
8
9
10
11
12
my_list = [1, 2, 3, 4, 5]
print(my_list[1:4])
# Output: [2, 3, 4] (elements from index 1 to 3)
print(my_list[:3])
# Output: [1, 2, 3] (elements from the beginning up to index 2)
print(my_list[2:])
# Output: [3, 4, 5] (elements from index 2 to the end)
print(my_list[::2])
# Output: [1, 3, 5] (every second element)
Copied!
xxxxxxxxxx
thislist = ["1.apple", "2.banana", "3.cherry", "4.orange", "5.kiwi", "6.melon", "7.mango"]
# thislist[starting index(including) : end index(not including):step]
print(thislist[:]) #Output: ['1.apple', '2.banana', '3.cherry', '4.orange', '5.kiwi', '6.melon', '7.mango']
print(thislist[1:]) #Output: ['2.banana', '3.cherry', '4.orange', '5.kiwi', '6.melon', '7.mango']
print(thislist[:5]) #Output: ['1.apple', '2.banana', '3.cherry', '4.orange', '5.kiwi']
print(thislist[2:5]) #Output: ['3.cherry', '4.orange', '5.kiwi']
print(thislist[::3]) #Output: ['1.apple', '4.orange', '7.mango']
print(thislist[1::2]) #Output: ['2.banana', '4.orange', '6.melon']
print(thislist[1:6:3]) #Output: ['2.banana', '5.kiwi']
xxxxxxxxxx
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
Slice position: 0 1 2 3 4 5 6
Index position: 0 1 2 3 4 5
>>> p = ['P','y','t','h','o','n']
# Why the two sets of numbers:
# indexing gives items, not lists
>>> p[0]
'P'
>>> p[5]
'n'
# Slicing gives lists
>>> p[0:1]
['P']
>>> p[0:2]
['P','y']
xxxxxxxxxx
# array[start:stop:step]
# start = include everything STARTING AT this idx (inclusive)
# stop = include everything BEFORE this idx (exclusive)
# step = (can be ommitted) difference between each idx in the sequence
arr = ['a', 'b', 'c', 'd', 'e']
arr[2:] => ['c', 'd', 'e']
arr[:4] => ['a', 'b', 'c', 'd']
arr[2:4] => ['c', 'd']
arr[0:5:2] => ['a', 'c', 'e']
arr[:] => makes copy of arr
xxxxxxxxxx
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"
xxxxxxxxxx
# List slicing
friends = ["Harry", "Tom", "Rohan", "Sam", "Divya", 45]
print(friends[0:4])
print(friends[-4:])
xxxxxxxxxx
# List slicing in Python
my_list = ['p','r','o','g','r','a','m','i','z']
# elements from index 2 to index 4
print(my_list[2:5])
# elements from index 5 to end
print(my_list[5:])
# elements beginning to end
print(my_list[:])
xxxxxxxxxx
In Python, we perform slicing using the following syntax: [start: end].
fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]
sliced_fruits = fruits[1:3]
print(sliced_fruits) # ["Orange", "Lemon"]
Slicing in Python is very flexible. If we want to select the first n elements
of a list or the last n elements in a list, we could use the following code:
fruits[:n]
fruits[-n:]
fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]
print(fruits[:3])
# ["Banana", "Orange", "Lemon"]
print(fruits[-2:])
# ["Apple", "Mango"]
xxxxxxxxxx
a = [1,2,3,4,5]
a[m:n] # elements grrater than equal to m and less than n
a[1:3] = [2,3]
xxxxxxxxxx
# Initialize list
Lst = [50, 70, 30, 20, 90, 10, 50]
# Display list
print(Lst[::])
xxxxxxxxxx
The basic rules of slice are:
I'''
The slice generates index/integers from - start, start + step, start +
step + step, and so on. All the numbers generated must be less than
the stop value when step is positive.'''
II'''
If step value is missing then by default is taken to be 1 '''
III'''
If start value is missing and step is positive then start value is by default
taken as 0.'''
IV'''
If stop value is missing and step is positive then start value is by
default taken to mean till you reach the ending index(including the
ending index)'''
V'''
A negative step value means the numbers are generated in
backwards order i.e. from - start, then start - step, then start -step
-step and so on. All the numbers generated in negative step must
be greater than the stop value.'''
VI'''
If start value is missing and step is negative then start value takes default
value -1'''
VII'''
If stop value is missing and step is negative then stop value is by
default taken to be till you reach the first element(including the 0
index element)'''