Python Built-in Methods – Python slice() Function

Python slice() Function

Returns a slice object to slice a sequence

Usage

The slice() function returns a slice object. A slice object is used to specify how to slice a sequence (list, tuple, string or range etc.) .

You can specify where to start the slicing, where to stop and specify the step.

Syntax

slice(start,stop,step)

Parameter Condition Description
start Optional A number to specify start of the slicing.
Default is 0.
stop Required A number to specify end of the slicing.
step Optional A number to specify the step.
Default is 1.

slice(stop)

When you call slice() with just one argument, you can slice a sequence from index 0 up to the specified index.

# Slice first 5 list items
L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
x = slice(5)
print(L[x])
# Prints ['a', 'b', 'c', 'd', 'e']
Python slice() - Specifying stop

Note that the item at index 5 ‘ f ‘ is not included.

slice(start, stop)

The slicing starts from 0 by default. However, you can start the slice at another number by adding a start parameter.

# Slice from index 2 to 7
L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
x = slice(2, 7)
print(L[x])
# Prints ['c', 'd', 'e', 'f', 'g']
Python slice() - Specifying start and stop

slice(start, stop, step)

The step of the slicing is 1 by default. However, you can specify a different step size by adding a step parameter.

# Returns every 2nd item between position 2 to 7
L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
x = slice(2, 7, 2)
print(L[x])
# Prints ['c', 'e', 'g']
Python slice() - Specifying Step Size

Slice with Negative Indices

You can specify negative indices while slicing a list.

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
x = slice(-7, -2)
print(L[x])
# Prints ['c', 'd', 'e', 'f', 'g']

You can specify both positive and negative indices at the same time.

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
x = slice(2, -2)
print(L[x])
# Prints ['c', 'd', 'e', 'f', 'g']

You can even specify a negative step size.

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
x = slice(7, 2, -2)
print(L[x])
# Prints ['h', 'f', 'd']

Reverse a Sequence

You can reverse a sequence by specifying both start and stop indices as ‘None‘ and a step as -1.

L = ['a', 'b', 'c', 'd']
x = slice(None, None, -1)
print(L[x])
# Prints ['d', 'c', 'b', 'a']

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.