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
# 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
# List slicing
friends = ["Harry", "Tom", "Rohan", "Sam", "Divya", 45]
print(friends[0:4])
print(friends[-4:])
xxxxxxxxxx
list_example = ["python","ruby","java","javascript","c#","css","html"]
print(list_example[3])#javascript
print(list_example[0])#python
print(list_example[6])#html
print(list_example[0:3]) #will print the programming language under python and javascript
print(list_example[:3]) all languages under python to javascript
xxxxxxxxxx
# define a list
my_list = [1, 2, 3, 4, 5]
# get a slice of the list
sublist = my_list[1:3]
print(sublist) # [2, 3]
# get a slice from the beginning of the list
sublist = my_list[:3]
print(sublist) # [1, 2, 3]
# get a slice from the end of the list
sublist = my_list[3:]
print(sublist) # [4, 5]
# get a slice from the middle of the list
sublist = my_list[1:-1]
print(sublist) # [2, 3, 4]
# get the last two elements of the list
sublist = my_list[-2:]
print(sublist) # [4, 5]
# get all elements except the last two
sublist = my_list[:-2]
print(sublist) # [1, 2, 3]
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
# 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[:])
#['o', 'g', 'r']
#['a', 'm', 'i', 'z']
#['p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z']
xxxxxxxxxx
#slicing arrays:
#generic sampling is done by
#arr[start:end] -> where start is the starting index and end is ending idx
>>> import numpy as np
>>> arr = np.array([1,2,3,4,5])
>>> print(arr[1:5]) #starting idx 1 to ending index 4
[2 3 4 5]#it will print from starting idx to ending idx-1
#if you leave the ending index blank it will print all
#from the starting index till end
>>> arr = np.array([2,6,1,7,5])
>>> print(arr[3:])
[7 5]
>>> print(arr[:3]) #if you leave the starting index blank it will print from 0 index to the ending idx-1\
[2 6 1]
>>> print(arr[:])
[2 6 1 7 5]
#leaving both the index open will print the entire array.
##########STEP slicing########
#if you want to traverse by taking steps more than 1
#we use step slicing in that case
#syntax for step slicing is : arr[start:end:step]
>>> arr = np.array([2,6,1,7,5,10,43,21,100,29])
>>> print(arr[1:8:2])#we have taken steps of two
[ 6 7 10 21]