How does slicing work in python list?

Slicing is a concept to carve out a substring from a given string. Use slicing notation s[start:stop:step] to access every step-th element starting from index start (included) and ending in index stop (excluded). All three arguments are optional, so you can skip them to use the default values (start=0, stop=len(lst), step=1). For example, the expression s[2:4] from string 'hello' carves out the slice 'll' and the expression s[:3:2] carves out the slice 'hl'.

Slicing is a Python-specific concept for carving out a range of values from sequence types such as lists or strings.

Try It Yourself:

It is one of the most popular Python features. To master Python, you must master slicing first. Any non-trivial Python code base relies on slicing. In other words, the time you invest now in mastering slicing will be repaid a hundredfold during your career.

Here’s a 10-min video version of this article--that shows you everything you need to know about slicing:

The Ultimate Guide to Slicing in Python

[Intermezzo] Indexing Basics

To bring everybody on the same page, let me quickly explain indices in Python by example. Suppose, we have a string ‘universe’. The indices are simply the positions of the characters of this string.

Index 0 1 2 3 4 5 6 7
Character u n i v e r s e

The first character has index 0, the second character has index 1, and the i-th character has index i-1.

The idea of slicing is simple. You carve out a subsequence from a sequence by defining the start and end indices. But while indexing retrieves only a single character, slicing retrieves a whole substring within an index range.

Use the bracket notation for slicing with the start and end position identifiers. For example, word[i:j] returns the substring starting from index i (included) and ending in index j (excluded). Forgetting that the end index is excluded is a common source of bugs.

You can also skip the position identifier before or after the slicing colon. This indicates that the slice starts from the first or last position, respectively. For example, word[:i] + word[i:] returns the same string as word.

Python puzzle 1: What is the output of this code snippet?

x = 'universe'
print(x[2:4])

The Step Size in Slicing

For the sake of completeness, let me quickly explain the advanced slicing notation [start:end:step]. The only difference to the previous notation is that it allows you to specify the step size as well. For example, the command ‘python'[:5:2] returns every second character up to the fourth character, i.e., the string ‘pto’.

Python puzzle 2: What is the output of this code snippet?

x = 'universe'
print(x[2::2])

Overshooting Indices in Slicing

Slicing is robust even if the end index shoots over the maximal sequence index. Just remember that nothing unexpected happens if slicing overshoots sequence indices. Here is an example.

word = "galaxy" 
print(word[4:50]) 

Summary Python Slicing

Short recap, the slice notation s[start:end:step] carves out a substring from s. The substring consists of all characters between the two characters at the start index (inclusive) and the end index (exclusive). An optional step size indicates how many characters are left out from the original sequence. Here is an example:

s = 'sunshine'
print(s[1:5:2])
#'us'
print(s[1:5:1])
#'unsh'

Learn Slicing By Doing

Ok, so let’s train slicing a little bit. Solve the following puzzle in your head (and check the solution below).

Python Puzzle 3: What is the output of this code snippet?

# (Shakespeare)
s = "All that glitters is not gold"
print(s[9:-9])
print(s[::10])
print(s[:-4:-1])

Frequently Asked Questions

Let’s go a bit deeper into slicing to make sure that you are getting it 100%.

I have searched Quora to find all the little problems new Python coders are facing with slicing. I will answer six common questions next.

1) How to skip slicing indices (e.g. s[::2])?

The Python interpreter assumes certain default values for s[start:stop:step]. They are: start=0, stop=len(s), and step=1 (in the slice notation: s[::]==s[0:len(s):1]).

2) When to use the single colon notation (e.g. s[:]) and when double colon notation (e.g. s[::2])?

A single colon (e.g. s[1:2]) allows for two arguments, the start and the end index. A double colon (e.g. s[1:2:2]) allows for three arguments, the start index, the end index, and the step size. If the step size is set to the default value 1, we can use the single colon notation for brevity.

3) What does a negative step size mean (e.g. s[5:1:-1])?

This is an interesting feature in Python. A negative step size indicates that we are not slicing from left to right, but from right to left. Hence, the start index should be larger or equal than the end index (otherwise, the resulting sequence is empty).

4) What are the default indices when using a negative step size (e.g. s[::-1])?

In this case, the default indices are not start=0 and end=len(s) but the other way round: start=len(s)-1 and end=-1. Note that the start index is still included and the end index still excluded from the slice. Because of that, the default end index is -1 and not 0.

5) We have seen many examples for string slicing. How does list slicing work?

Slicing works the same for all sequence types. For lists, consider the following example:

l = [1, 2, 3, 4]
print(l[2:])
# [3, 4]

Slicing tuples works in a similar way.

6) Why is the last index excluded from the slice?

The last index is excluded because of two reasons. The first reason is language consistency, e.g. the range function also does not include the end index. The second reason is clarity, e.g., here’s an example why it makes sense to exclude the end index from the slice.

customer_name = 'Hubert'
k = 3 # maximal size of database entry
x = 1 # offset
db_name = customer_name[x:x+k]

Now suppose the end index would be included. In this case, the total length of the db_name substring would be k + 1 characters. This would be very counter-intuitive.


Puzzle Solutions

Here are the solutions to the puzzles in this article.

Puzzle 1:

iv

ies

Puzzle 3:

glitters is
Al
dlo


Subscribe to *FREE* Python Training Newsletter

And become a Python master on auto-pilot.

How does slicing work in python list?

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.

When slicing in Python What does the 2 in [:: 2 specify?

Therefore, the elements before the stop sign are returned. Second note, when no start is defined as in A[:2] , it defaults to 0. There are two ends to the list: the beginning where index=0 (the first element) and the end where index=highest value (the last element).

What is the purpose of list slicing?

List slicing can be used to modify lists or even delete elements from a list.

Does list slicing create a new list?

No, slicing returns a list which is inside the original list. Not a copied list. But, in your code; there is a function which edits the parameter-list.

Does slicing copy the list Python?

Slicing lists does not generate copies of the objects in the list; it just copies the references to them. That is the answer to the question as asked.

What is the difference between slicing and indexing of list?

“Indexing” means referring to an element of an iterable by its position within the iterable. “Slicing” means getting a subset of elements from an iterable based on their indices.

What is slicing in Python explain list and string with slicing?

Slicing Strings You can return a range of characters by using the slice syntax. Specify the start index and the end index, separated by a colon, to return a part of the string.