Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
list
slicing
indexing

What is slicing in Python?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Lists are an integral feature of Python. The language provides a vast range of different utilities which make list manipulation simple and efficient.

With the introduction of slicing, a sublist can be easily accessed using the indexing convention.

Slicing a list/string

A list can be sliced in the following way:

myList[start:end]

Keep in mind that start and end are indices of the list. The value at the end index is not included in the sublist. Slicing also works with strings.

svg viewer
myList = range(10) # A list from 0 to 9
print myList
print myList[2:7]
myString = "Hello World"
print myString
print myString[6:11] # Accessing the "World" substring
svg viewer

Specifying a single boundary

It is not necessary to define the start and end indices explicitly.

If the end is left blank, the resulting sublist would start from the start index and end at the last element in the list.

If the start index is omitted instead, the sublist would begin from the first element and stop at the end index, but not including the element at the end index.

myList = range(10) # A list from 0 to 9
print myList
print myList[3:]
print myList[:6]
print myList[:] # Prints the whole list
myString = "Hello World"
print myString
print myString[4:]
print myString[:8]

Stepped indexing

So far, the sublists have only contained continuous elements. However, a step can be defined which tells the compiler to “step” over certain elements.

The step is defined after the end index:

 myList[start:end:step]
myList = range(10)
print myList
print myList[0:10:3] # Steps three elements ahead each time
print myList[4::2] # Omit the end index and step two elements ahead

Modifying a sublist

Assigning new values to a sliced sublist will alter the content of the original list as well.

myList = range(10)
print myList
myList[2:5] = [41, 83, 17]
print myList

Reversed sublist

A reversed sublist or substring can be obtained by switching the start and end indices. In this case, a negative step would have to be provided:

svg viewer
myList = range(10)
print myList
# The reversed list from the 9th index to the 5th index
print myList[9:5:-1]

Negative slicing

A negative index begins from the end of the list. So, for a list of 10 elements, list[-2] would imply list[10-2]

myList = range(10)
print myList
print myList[-3:] # Starts from the 7th index and goes till the end
print myList[-3::-1] # Starts from the 7th index and goes till the end

RELATED TAGS

python
list
slicing
indexing
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring