How to slice lists in Python?
We can access a range of items in a list by using the slicing operator (colon).
print(n1[1:3])
#counting starts from 0
#will display elements from start to position -1
Example:
n1=[‘c’ , ‘o’ , ‘m’ , ‘p’ , ‘u’ , ‘t’ , ‘e’ , ‘r’]
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
c |
o |
m |
P |
u |
t |
e |
R |
n1=[‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’]
print(n1)
output: [‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
print(n1[1:3])
output: [‘o’, ‘m’]
print(n1[1:4])
output : [‘o’, ‘m’, ‘p’]
print(n1[1:5])
output: [‘o’, ‘m’, ‘p’, ‘u’]
print(n1[2:3])
output: [‘m’]
print(n1[2:4])
output: [‘m’, ‘p’]
print(n1[2:5])
output: [‘m’, ‘p’, ‘u’]
n1=['c','o','m','p','u','t','e','r'] print(n1) print(n1[1:3]) print(n1[1:4]) print(n1[1:5]) print(n1[2:3]) print(n1[2:4]) print(n1[2:5])
Output:
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
[‘o’, ‘m’]
[‘o’, ‘m’, ‘p’]
[‘o’, ‘m’, ‘p’, ‘u’]
[‘m’]
[‘m’, ‘p’]
[‘m’, ‘p’, ‘u’]
Example: 1
Give output:
n2=[‘c’,’a’,’t’,’a’,’l’,’y’,’s’,’t’]
Positive indexing
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
c |
a |
t |
a |
l |
y |
s |
t |
-8 |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Negative indexing
print(n2)
print(n2[:4])
print(n2[1:4])
print(n2[:-4])
print(n2[:6])
print(n2[:-6])
print(n2[1:6])
print(n2[3:6])
print(n2[2:6])
print(n2[:-2])
output:
[‘c’, ‘a’, ‘t’, ‘a’, ‘l’, ‘y’, ‘s’, ‘t’]
[‘c’, ‘a’, ‘t’, ‘a’]
[‘a’, ‘t’, ‘a’]
[‘c’, ‘a’, ‘t’, ‘a’]
[‘c’, ‘a’, ‘t’, ‘a’, ‘l’, ‘y’]
[‘c’, ‘a’]
[‘a’, ‘t’, ‘a’, ‘l’, ‘y’]
[‘a’, ‘l’, ‘y’]
[‘t’, ‘a’, ‘l’, ‘y’]
[‘c’, ‘a’, ‘t’, ‘a’, ‘l’, ‘y’]
n2=['c','a','t','a','l','y','s','t'] print(n2) print(n2[:4]) print(n2[1:4]) print(n2[:-4]) print(n2[:6]) print(n2[:-6]) print(n2[1:6]) print(n2[3:6]) print(n2[2:6]) print(n2[:-2])
Output:
[‘c’, ‘a’, ‘t’, ‘a’, ‘l’, ‘y’, ‘s’, ‘t’]
[‘c’, ‘a’, ‘t’, ‘a’]
[‘a’, ‘t’, ‘a’]
[‘c’, ‘a’, ‘t’, ‘a’]
[‘c’, ‘a’, ‘t’, ‘a’, ‘l’, ‘y’]
[‘c’, ‘a’]
[‘a’, ‘t’, ‘a’, ‘l’, ‘y’]
[‘a’, ‘l’, ‘y’]
[‘t’, ‘a’, ‘l’, ‘y’]
[‘c’, ‘a’, ‘t’, ‘a’, ‘l’, ‘y’]
Example: Give output
n1=[‘c’ , ‘o’ , ‘m’ , ‘p’ , ‘u’ , ‘t’ , ‘e’ , ‘r’]
print(n1)
print(n1[:])
print(n1[0:])
print(n1[1:])
print(n1[2:])
print(n1[3:])
print(n1[4:])
print(n1[5:])
output:
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
[‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
[‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
[‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
[‘u’, ‘t’, ‘e’, ‘r’]
[‘t’, ‘e’, ‘r’]
n1=['c' , 'o' , 'm' , 'p' , 'u' , 't' , 'e' , 'r'] print(n1) print(n1[:]) print(n1[0:]) print(n1[1:]) print(n1[2:]) print(n1[3:]) print(n1[4:]) print(n1[5:])
Output:
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
[‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
[‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
[‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
[‘u’, ‘t’, ‘e’, ‘r’]
[‘t’, ‘e’, ‘r’]
Example:
my_list = [‘c’ , ‘o’ , ‘m’ , ‘p’ , ‘u’ , ‘t’ , ‘e’ , ‘r’]
print(my_list[2:5])
print(my_list[:-5])
print(my_list[5:])
print(my_list[:])
Output:
[‘m’, ‘p’, ‘u’]
[‘c’, ‘o’, ‘m’]
[‘t’, ‘e’, ‘r’]
# elements beginning to end
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
my_list = ['c','o','m','p','u','t','e','r'] print(my_list[2:5]) print(my_list[:-5]) print(my_list[5:]) print(my_list[:])
Output:
[‘m’, ‘p’, ‘u’]
[‘c’, ‘o’, ‘m’]
[‘t’, ‘e’, ‘r’]
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]