Python slice() Function

The slice() is a built-in function in Python that is used to slice sequences (string, tuple, list, range, or bytes) and returns a slice object. We can extract a particular range of elements from the sequence objects using the slice() function. Alternatively, you can also use the slice notation.

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.

In this article, we will see slice() syntax, its parameters, and how to use slice objects by using all its parameters separately with examples.

1. Quick Examples of slice() Function

Following are quick examples of how to use the slicing technique and slice() function to extract a list of elements.


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

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

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

# Using slice() with all parameters
print(countries[slice(2,6,3)])

# All elements
print(countries[:])

# With start value
print(countries[0:])

# With end value
print(countries[:len(countries)])

# With step
print(countries[0:len(countries):2])

# Particular range 
print(countries[2:5])

# Negative slicing
print(countries[-5:-2])


2. Python slice() function

The slice() function in Python is a built-in function that is used to slice the sequence objects. This will take three parameters. i.e start, stop and step. start will get the elements from the starting range specified and the end will return the elements up to the specified range. the step is used to increment the index within the start and end range.

2.1 slice() Syntax

Following is the syntax of the slice()


# Syntax of slice() function
slice(start,stop,step)

2.2 slice() Parameters

  • start – Index of the first element to include in the slice (inclusive). The default is 0.
  • stop – Index of the last element to exclude from the slice (exclusive). The default is the end of the tuple.
  • step – Step size between each element in the slice. The default is 1.

3. Using slice() on List

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.

Example 3: With all parameters.


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

# Using slice() with all parameters
print(countries[slice(2,6,3)])

# Output:
# ['UK', 'Egypt']

Here, we specified the start as 0, end as 6, and step as 3. So the elements from starting index 2 to item 6 are returned in a list by step 3. The sliced list has ‘UK’ and ‘Egypt’.

4. List Slicing using []

Here, we will use the traditional slicing notation [] to extract the list of elements by the specified range. Indexing starts from 0.

By passing different parameters, we can return a particular list of elements. Basically, slicing will take three values i.e start, end, and index-jump. start will get the elements from the starting range specified and the end will return the elements up to the specified range. index-jump is used to increment the index within the start and end range.

4.1 Slice [] Syntax

Let’s see how to perform List Slicing.


# Syntax of slicing
inp_list[start:end:indexjump]

Here, inp_list is the input list.

4.2 Slicing Examples

Example 1: With no specification of values


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

# All elements
print(countries[:])

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

Here, we didn’t specify any value which means the start will be considered as 0 and the end will be considered as the size of the list, So all the list of all elements was returned.

Example 2: With start value only.


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

# With start value
print(countries[0:])

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

Here, we specified only the start value – 0. So It fetched from the 0th index position element to the last item in a list.

Example 3: With end value only.


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

# With end value
print(countries[:len(countries)])

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

Here, we specified only the end value = length of the list. So It fetched from the 0th index position element to the last item in the list.

Example 4: With all parameters.


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

# With jump
print(countries[0:len(countries):2])

# Output:
# ['China', 'UK', 'Italy']

Here, we specified as start – 0, end as size of the list along with indexjump value as 2. It will fetch from first to last with jump of 2 (skip 2 at each time).

The final list of elements are – ‘China’, ‘UK’, ‘Italy’.

Example 5: Particular range


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

# Particular range 
print(countries[2:5])

# Output:
# ['UK', 'USA', 'Italy']

Here, we specified as start 2 and end as 5. So the list will return elements present at index 2 to index 4 (Item – 5).

The final list of elements are ‘UK’, ‘USA’, ‘Italy’.

Example 6: Negative Slicing


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

# Negative slicing
print(countries[-5:-2])

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

Here, we specified as start as -5 and the end as -2. So the elements returned are – ‘India’, ‘UK’, ‘USA’.

5. Slice a Tuple with Specific Start, End and Step

Similarly, let’s slice a tuple with all parts start, stop, and step. The start position is the index of the first element you want to include in the slice, the stop position is the index of the element you want to exclude from the slice, and the step is the increment between each element.

Here is an example of a slicing tuple.


# Slice a tuple with specific start and end positions, step
tuples = ('Spark','Python','Pandas','Pyspark','Java','Hadoop', 'C++')
start = 1
stop = 6
step = 2
slice1 = slice(start, stop, step)
result = tuples[slice1]
print(result)

# Output
# ('Python', 'Pyspark', 'Hadoop')

# Slice from index 2 to 
# index 5(exclusive) with a step of 2
result = tuples[2:5:2]
print(result)

# Output
# ('Pandas', 'Java')

You can also use the start, stop, and step with negative values. Negative values count from the end of the tuple. For example, the slice starts at the index -4 (the fourth element from the end) and continues to the end of the tuple, with a step of 1 (i.e., including every element).


# Slice the last four elements 
# with a step of 1
tuples = ('Spark','Python','Pandas','Pyspark','Java','Hadoop')
result = tuples[-4::1]
print(result)

# Output
# ('Pandas', 'Pyspark', 'Java', 'Hadoop')

6. Conclusion

In this article, you have learned the Python slice() function that is used to slice the sequence objects. Also, learned python slicing notation with examples. Both approaches will take the same parameters and the functionality is the same.

Related Articles

Leave a Reply