You are currently viewing Python List Slice with Examples

How to slice a list into multiple lists in Python? You can easily slice a list in Python either by using the slice() function or slice notation. These both returns a new list that is a subset of the original list. Both these options takes start, stop and step values to split the list.

Advertisements

In this article, I will explain the syntax of the slice() built-in function and slice notation, and their parameters and explain how to use it.

1. Quick Examples of Slicing a List

If you are in a hurry, below are some quick examples of how to slice a list.


# Quick examples of slicing a list

# Initialize list
lists = ['Spark','Python','Pandas','Pyspark','Java','Hadoop']

# Example 1: List slicing using positive indexes
result = lists[1:3]

# Example 2: Use len() function
result = lists[3:len(lists)]
print(result)

# Example 3: Slice list with negative indexes
result = lists[-4:]
print(result)

# Example 4: Use a slice list with negative indexes
result = lists[-4::1]
print(result)

# Example 5: Slice the list with positive
#  & negative indexes
result = lists[1:-3]

# Example 6: Slice the list using a specific step of the slicing
lists = [0, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90]
result = lists[2:7:3]

# Example 7: Slice the list in reverse order
result = lists[8:0:-3]

# Example 8: Slice list using reverse a list 
result = lists[::-1]

# Example 9: Slice from the beginning 
# to index 4 (exclusive) 
result = lists[:4]

# Example 9: Slice from index 6 
# to the end of the list
result = lists[6:]

# Example 10: Slice the entire list 
# with a step of 3
result = lists[::3]

# Example 11: Slice list 
# Using insert multiple list items
lists = [0, 5, 10, 20, 30, 40, 50]
lists[2:2] = [1, 2, 3]

# Example 12: Insert at the end
lists[len(lists):] = [1, 2, 3]

# Example 13: Slice list using delete multiple list items
lists = ['Spark','Python','Pandas','Pyspark','Java','Hadoop']
del lists[3:6]

# Example 14: Delete multiple list items
lists[1:5] = []

# Example 15: Slice list using modify multiple list values
lists = ['Spark','Python','Pandas','Pyspark','Java','Hadoop']
lists[1:4] = ['C++', 'Hyperion', 'NumPy']

# Example 16: Clone or copy a list
lists = [5, 10, 20, 30, 40]
result = lists.copy()

2. Syntax of Slicing Operator

Following is the syntax of the list slice.


# Syntax of a slice
list[start:stop:step]

2.1 Parameters of Slice

  • start – Index of the first element to include in the slice (inclusive). If not specified, it defaults to 0.
  • stop – Index of the first element to exclude from the slice (exclusive). The default is the end of the list.
  • step – Step size between each element in the slice. The default is 1.

3. Python Slice List Examples using Notation

You can slice a list in Python using positive indexes, which refer to the position of the elements from the beginning of the list. Following are examples of using the slice notation to slice a list in Python, here I will use the combination of start, stop and step values with examples.

3.1 Slice List with Positive Indexes in Python

For example, you can use the slice operator [1:3] to extract a subset of the list containing elements with indexes 1 and 2. From the below example it returns 'Python' and 'pandas'.


# List slicing using positive indexes
lists = ['Spark','Python','Pandas','Pyspark','Java','Hadoop']
result = lists[1:3]
print(result)

# Output:
# ['Python', 'Pandas']

Note that the slice operator [start:stop] extracts elements from start up to (but not including) stop.

You can also use the len() function to get the length of the list and slice from a certain index up to the end of the list. For example, you can use the slice operator [3:len(lists)] to extract a subset of the list containing elements from index 3 up to the end of the list.


# Use len() function
lists = ['Spark','Python','Pandas','Pyspark','Java','Hadoop']
result = lists[3:len(lists)]
print(result)

Yields below output.


# Output:
['Pyspark', 'Java', 'Hadoop']

3.2 Slice List with Negative Indexes

You can also slice a list with negative indexes in Python. Negative indexes refer to the position of the elements from the end of the list. For example, you used the slice operator [-4:] to extract a subset of the list containing elements from the fourth-to-last element up to the end of the list. From the example below, it return values 'Pandas', 'Pyspark', 'Java', and 'Hadoop'.


# Slice list with negative indexes
lists = ['Spark','Python','Pandas','Pyspark','Java','Hadoop']
result = lists[-4:]
print(result)

# Use slice list with negative indexes
result = lists[-4::1]
print(result)

# Output:
# ['Pandas', 'Pyspark', 'Java', 'Hadoop']

3.3 Slice the List with Positive & Negative Indexes

You can also slice a list using both positive and negative indexes in Python. Positive indexes start from 0 and increase by 1, while negative indexes start from -1 and decrease by 1. To slice a list with positive and negative indexes, you need to specify the start and end indexes of the slice. For example, starts from the index 1 ('Python') and ends at the index -3 (exclusive) ('Pandas').


# Slice the list with positive & negative indexes
lists = ['Spark','Python','Pandas','Pyspark','Java','Hadoop']
result = lists[1:-3]
print(result)

# Output:
# ['Python', 'Pandas']

3.4 Slice List by Specifying step Value

To slice a list using a specific step size in Python, you can use the syntax [start:stop:step] where start is the index of the first element to include in the slice (inclusive), stop is the index of the last element to include in the slice (exclusive), and step is the number of elements to skip between each included element.

For example, let’s use the start as 2, stop as 7 (which includes all elements up to index 90), and step as 3 in the below example. This creates a new list that contains every other element from the original list, starting with the first element.


# Slice list using specify step of the slicing
lists = [0, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90]
result = lists[2:7:3]
print(result)

# Output:
# [10, 40]

You can also use a negative step size to slice the list in reverse order. For example, the start is 8, the stop is 0 (which includes all elements up to index 1), and the step is -3. This creates a new list that contains every other element from the original list, starting with the last element and moving backward.


# Slice the list in reverse order
lists = [0, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90]
result = lists[8:0:-3]
print(result)

# Output:
#  [70, 40, 10]

3.5 Slice List Using Reverse a List

To reverse a list in Python, you can use the slicing syntax with a step size of -1. For example, lists[::-1] creates a new list that contains all elements of lists, but in reverse order. The start and stop indices are omitted, which means the slice starts at the first element and goes up to the last element, and the step is -1, which means the slice goes backward.


# Slice list using reverse a list 
lists = [0, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90]
result = lists[::-1]
print(result)

# Output:
# [90, 80, 70, 60, 50, 40, 30, 20, 10, 5, 0]

3.6 Slice at the Beginning & End

To slice from the beginning of the list, you can omit the start value in the slice syntax: lists[:stop:step]. For example, you slice from the beginning of the list until index 4 (exclusive), which gives us the first four elements of the list.


# Slice from the beginning 
# to index 4 (exclusive) 
lists = [0, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90]
result = lists[:4]
print(result)

# Output:
# [0, 5, 10, 20]

To slice until the end of the list, you can omit the stop value in the slice syntax: lists[start::step]. For example, you slice from the index 6 until the end of the list, which gives us the last five elements of the list.


# Slice from index 6 to the end of the list
result = lists[6:]
print(result)

# Output :
# [50, 60, 70, 80, 90]

To slice the entire list, you can omit both the start and stop values in the slice syntax: lists[::step]. For example, you slice the entire list with a step of 3, which gives us every other element of the list.


# Slice the entire list with a step of 3
result = lists[::3]
print(result)

# Output:
# [0, 20, 50, 80]

3.7 Using Slice Insert Elements in the Middle of List

You can insert items into a list without replacing anything by specifying a zero-length slice.


# Slice list 
# Using insert multiple list items
lists = [0, 5, 10, 20, 30, 40, 50]
lists[2:2] = [1, 2, 3]
print(lists)

# Output
# [0, 5, 1, 2, 3, 10, 20, 30, 40, 50]

# Insert at the end
lists = [0, 5, 10, 20, 30, 40, 50]
lists[len(lists):] = [1, 2, 3]
print(lists)

# Output
# [0, 5, 10, 20, 30, 40, 50, 1, 2, 3]

In this example, we are inserting the items [1, 2, 3] into the list lists at the index 2 without replacing any existing elements. The lists[2:2] slice notation specifies a zero-length slice starting and ending at the index 2. This represents the empty space between elements 2 and 3 in the list. you then use the = operator to assign the new list [1, 2, 3] to this empty slice, which inserts the new items at the specified position in the list.

3.8 Slice the List Using Delete Multiple List Items

To slice a list using the del statement to delete multiple items, you can specify a range of indices to delete. For example, lists[3:6] specify a range of indices to delete, starting from the index 3 and ending at the index 5 (inclusive). The del statement then removes those items from the list.


# Slice list using delete multiple list items
lists = ['Spark','Python','Pandas','Pyspark','Java','Hadoop']
del lists[3:6]
print(lists)

# Output:
# ['Spark', 'Python', 'Pandas']

# Delete multiple list items
lists[1:5] = []
print(lists)

# Output:
# ['Spark', 'Hadoop']

3.9 Slice List Using Modify Multiple List Values

To slice a list using assignment to modify multiple values, you can also specify a range of indices to modify. For example, lists[1:4] specify a range of indices to modify, starting from the index 1 and ending at the index 3 (inclusive). The assignment statement then sets the values of those items to the list ['C++', 'Hyperion', 'NumPy'].


# Slice list using modify multiple list values
lists = ['Spark','Python','Pandas','Pyspark','Java','Hadoop']
lists[1:4] = ['C++', 'Hyperion', 'NumPy']
print(lists)

# Output:
# ['Spark', 'C++', 'Hyperion', 'NumPy', 'Java', 'Hadoop']

3.10 Clone or Copy a List

To clone or copy a list in Python, you can use the copy() method or the slice syntax. For example, you first create an lists with five elements. Then, you use the copy() method to create a clone of the list and assign it to a new variable called the result.


# Clone or copy a list
lists = [5, 10, 20, 30, 40]
result = lists.copy()
print(result)

# Use clone list
result = lists[:]
print(result)

# Output: 
# [5, 10, 20, 30, 40]

4. Using slice() Function to Slice List

The slice() is a built-in function in Python that is used to slice list and returns a new sliced list with selected range of elements. We can extract a particular range of elements from the list objects using the slice() function.

Slicing is the general approach that will extract elements based on the index position of elements present in the sequence. Similarly, the slice() function also uses the index positions to return a range of elements.

Since the slice() function can be used on any sequence object, In this section, I will slice the list object with examples.

Example 1: With just the end parameter

Following is an example of slice() with just one parameter which is considered as the end value. By default, it uses 0 as the start value. Here, we specified only the end parameter i.e 4. So the elements from starting index to item – 4 returned in a list.


# Consider the list of countries
countries = ["China","India","UK","USA","Italy","Egypt"]

# Using slice() with end parameter
print(countries[slice(4)])

# Output:
# ['China', 'India', 'UK', 'USA']

Example 2: With start and end parameters.


# Consider the list of countries
countries = ["China","India","UK","USA","Italy","Egypt"]

# Using slice() with start and end parameters
print(countries[slice(0,4)])

# Output:
# ['China', 'India', 'UK', 'USA']

Here, we specified the start as 0 and the end parameter as 4. So the elements from starting index – 0 to item – 4 returned in a list.

Conclusion

In this article, I have explained the python list slice() method syntax, parameters, and how to use it to slice a list in python with examples. Also, I have explain slice notation with examples by using combination of start, stop and step values. These both return a new list that is a subset of the original list. Both these options takes start, stop and step values to split the list.

Slicing is the general approach that will extract elements based on the index position of elements present in the sequence.

Happy Learning !!