You are currently viewing Python Slice Notation Explain

Slice notation in Python is used for selecting a range of items from a sequence such as a list, tuple, or string. In this article, we’ll explore slice notation in detail and provide examples of how to use it in your Python code. By understanding slice notation, you’ll be able to work more effectively with sequences in your Python programs.

Advertisements

Related: Python slice() Builtin Function

1. Quick Examples of Slice Notation

Below is a list of examples that can give you a high-level overview of the Python Slice notation. We will see more details in upcoming sections.


# Slicing a List
my_list = [1, 2, 3, 4, 5]
my_list[1:4]
my_list[::2]   

# Slicing a Tuple
my_tuple = (1, 2, 3, 4, 5)
my_tuple[1:4]
my_tuple[::-1]


# Slicing a String
my_string = "sparkbyexamples"
my_string[5:8]
my_string[:5]  

# Slice Notation with Negative Indices
my_list = [1, 2, 3, 4, 5]
my_list[-3:] # [3, 4, 5]
my_tuple[:-3]    # (1, 2)

2. What is Slice Notation in Python?

Slice notation is a syntax feature in Python that allows you to extract a specific subset of a sequence. You can use slice notation with any sequence in Python, including strings, lists, and tuples.

Slice notation returns a new sequence that is a subset of the original sequence. You can use slice notation to extract subsets of a sequence in a variety of ways, such as by specifying the start and end indices, using negative indices to count from the end of the sequence, or specifying a step parameter to skip elements in the sequence.


# Get the first three elements
subset1 = my_list[:3]
print(subset1)  # Output: [1, 2, 3]

# Get the last two elements
subset2 = my_list[-2:]
print(subset2)  # Output: [4, 5]

# Get every other element
subset3 = my_list[::2]
print(subset3)  # Output: [1, 3, 5]

3. Syntax of Slice Notation

Slice notation is written using square brackets [] and takes up to three parameters separated by colons :. The three parameters are the start index, the stop index, and the step parameter.


# Syntax
my_list[start:stop:step]

Each parameter is optional, and the default values are as follows:

  • start: 0
  • stop: end of the list
  • step: 1

3.1 Parameters

  • start: Index of the first element to include in the slice. The default is 0.
  • stop: Index of the first element to exclude from the slice. The default is the end of the list.
  • step: Step size between each element in the slice. Default is 1.

4. List Slice Notation

We can use the slicing notation for the Python list. Slicing a list in Python means selecting a specific subset of items from a list. It can be done using slice notation in Python.

4.1 Example: Slice Notation [start:]

This example uses slice notation with only a start index specified. It returns a new list that includes all elements from the start index to the end of the list.


# Slice noation with start
my_list = [10, 20, 30, 40, 50]
subset = my_list[2:] 

# Output:
# [30, 40, 50]

4.2 Example: Slice Notation [start:stop]

Slice notation with both start and stop indices specified. It returns a new list that includes all elements between the start and stop indices, but does not include the element at the stop index.


# Slice notation with start & stop
subset = my_list[1:4] 

# Output:
# [20, 30, 40]

4.3 Example: Slice Notation [:stop]

Slice notation with only a stop index specified. It returns a new list that includes all elements from the beginning of the list up to, but not including, the element at the stop index.


# Slice notation with just stop
subset = my_list[:3] 
# [10, 20, 30]

4.4 Example: Slice Notation [::step]

In the following example, we use slice notation with only a step value specified. It returns a new list that includes every step-th element from the original list. In this case, the resulting list includes every other element of my_list.


# Slice notation with just step
subset = my_list[::2] 
# [10, 30, 50]

5. Tuple Slice Notation

The slicing syntax for tuples is identical to that of lists, but because tuples are immutable, slicing a tuple creates a new tuple rather than modifying the original one.

Python Tuples are similar to lists in many ways, but with one key difference: tuples are immutable, meaning they cannot be modified once they are created.


my_tuple = (10, 20, 30, 40, 50)
subset1 = my_tuple[2:]  # Returns (30, 40, 50)
subset2 = my_tuple[:3]  # Returns (10, 20, 30)
subset3 = my_tuple[::2]  # Returns (10, 30, 50)

6. String Slicing

In Python, strings are also sequences, which means you can use the slice notation to extract substrings from them in the same way as lists and tuples.


my_string = "sparkbyexamples"

# Slice from index 0 to 4
substring1 = my_string[0:5] # Returns "spark"

# Slice from index 5 to the end
substring2 = my_string[5:] # Returns "byexamples"

# Slice from index 5 to index 7
substring3 = my_string[5:8] # Returns  "bye"

# Slice from the beginning to index 7
substring4 = my_string[:8] # Returns "sparkbye"

# Slice from the beginning to the end, skipping every other character
substring5 = my_string[::2] # Returns "srbyeal"

7. Slice with Negative Indices

When using slice notation, negative indices can be used to specify the start and end points of the slice. A negative index for start indicates that the slice should start from the end of the sequence, and a negative index for stop indicates that the slice should end at a position counting from the end of the sequence.


# Slice from the end of the list to the third-to-last element
slice1 = my_list[-1:-4:-1] 
# Returns [5, 4, 3]

# Slice from the end of the list to the second-to-last element
slice2 = my_list[-1:-3:-1] 
# Returns [5, 4]

# Slice from the end of the list to the first element
slice3 = my_list[-1::-1] 
# Returns [5, 4, 3, 2, 1]

8. Reverse a sequence with Slice Notation

We can use slicing to reverse any sequence by using a step value of -1.


# Reverse sequence with Slice Notation
reversed_string = my_string[::-1]
print(reversed_string)  

reversed_list = my_list[::-1]
print(reversed_list)

# Output :
# selpmaxeybkraps
# [5, 4, 3, 2, 1]

9. Summary and Conclusion

Slice notation is a powerful feature of Python that enables us to extract a subset of a sequence object such as a list, tuple, or string. We covered the syntax of slice notation and provided examples of how to use it with different sequence types. Let me know if you have any questions.

Happy slicing!

Related Articles