Python – Slice List example

# Python – Slice List example

In our previous article, we came with examples to iterate list and iterate dictionary. Slicing is an important and commonly used feature in Python. By using slice operation at list, we can get sub-list from original list. In this tutorial we will demonstrate different ways to slice list.

### 1)  Most popular way to slice List

There are different ways to slice List like from start index, between start and end index or up to end index . Below example demonstrate all above points.

```def main():
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

#1. slice from start index
subList = lst[2:]
print("Sub list after index position 2 is", subList)

#2. slice between start and end points
subList = lst[3:5]
print("Sub list between index 3 to 5 is", subList)

#3. slice up to the end point(index)
subList = lst[:5]
print("Sub list up to index 5 is", subList)

if __name__ == '__main__':
main()```

In above example, we have a list which contains numbers from 1 to 10 from which sub lists are create as follow.

1. In example 1, sub list is created with start index 2.
2. In example 2, sub list is created between start index and end index which are 3 and 5.
3. In example 3, sub list is created with end index 5.

#### Output

```('Sub list after index position 2 is', [3, 4, 5, 6, 7, 8, 9, 10])
('Sub list between index 3 to 5 is', [4, 5])
('Sub list up to index 5 is', [1, 2, 3, 4, 5])```

### 2) Slice List in reverse

The above example slices the List in forward direction. However, One can slice list in reverse order. If we use index as -1 in list, the index is treated as end index of the list. So, by using negative sign before the index value you can slice list in reverse. Below example demonstrate slice list in reverse.

```def sliceReverse():
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

#1. get last two elements
subList = lst[-2:]
print("Sub list containing last 2 elements ", subList)
#2. get all excluding last 2 elements
subList = lst[:-2]
print("Sub list containing all elements except last 2 ", subList)

if __name__ == '__main__':
sliceReverse()```

#### Output

```('Sub list containing last 2 elements ', [9, 10])
('Sub list containing all elements except last 2 ', [1, 2, 3, 4, 5, 6, 7, 8])```

### 3) Slice List using slice Object

The `slice(start, stop, increment)` object initialization takes in 3 arguments with the last one being the optional index increment. Below example demonstrate list slicing with and without incremental value.

```def usingSlice():
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

#1. slice without increment
sliceObj = slice(2, 8)
subList = lst[sliceObj]
print("Slice list without increment value : ", subList)

#2. slice with increment
sliceObj = slice(2, 8, 2)
subList = lst[sliceObj]
print("Slice list with increment value 2 : ", subList)

if __name__ == '__main__':
usingSlice()```

#### Output

```('Slice list without increment value : ', [3, 4, 5, 6, 7, 8])
('Slice list with increment value 2 : ', [3, 5, 7])```

Stay tuned for updates and tutorials !!!