Python 3 Tutorial for Beginners #07 – Python 3 Sequence Slicing

Python 3 sequence slicing is a pythonic way of accessing a portion of a sequence. Slicing copies, the selected data to a new sequence of same type (Shallow copy).

First of all let’s define 3 variables as following:

a = ["I", "love", "python", "very", "much"]
b = ("give", "me", "tutorials")
c = ["only", "if", "good"]

First and third variables are lists with strings and the second one is a tuple with 3 strings. (This should be obvious to you if you followed the previous tutorial).

Let’s take the list a. I want to create a new list with objects “love” and “python“. To do that we need to create a new list with a[1] and a[2]. Let’s see how we can do that.

print(a[1:3])

Above code segment prints the desired slice. As you can see it is a slice of ‘a’ from index 1 to index 3 (exclusive).

Python 3 Sequence Slicing Syntax

Python 3 sequences support slicing in a beautiful and succinct way. First of all you need to understand how the negative indices work.

Negative Indices in Python 3 Sequences

Negative indices count from the end of sequence. It starts from -1.

Python 3 Sequence Negative Indices
Python 3 Sequence Negative Indices

Example: let’s take the list a, we want to get the last element of it.

print(a[-1])

Above code will print “much“. Now let’s try to get the second last element.

print(a[-2])

This will print “very“.

Sequence Slicing Syntax

Python 3 Sequence Slicing Syntax
Python 3 Sequence Slicing Syntax

Slicing requires that you specify at least a single colon character inside the square brackets.

Other Useful Functions

Python have various built-in functions that simplifies your workload. Let’s see few functions that you can use with sequences.

Useful Functions
Useful Functions
Function Usage
tuple(<sequence>) This function can create a tuple from a sequence.
Example:

d = tuple(a)
# ('I', 'love', 'python', 'very', 'much')
d = tuple()
# ()
list(<sequence>) This function can create a list from a sequence.
Example:

d = list(b)
# ['give', 'me', 'tutorials']
d = list()
# []
<str>.join(<string sequence>) Join a sequence of strings using another string.
Example:

" ".join(a)
# I love python very much
len(<sequence>) Get the length of a sequence.
Example:

len(a)
# Returns 5
Operator Join 2 sequences of a same type to a single sequence.
Example:

d = a + b
# ['I', 'love', 'python', 'very', 'much',
# 'only', 'if', 'good']

Exercises

Rest of the tutorial will be covered in exercises form.

Print last 2 elements of ‘a
print(a[-2:])
# ['very', 'much']
Get even indexed elements of ‘a‘ (Indexes 0, 2, 4 should be copied)
print(a[::2])
# ['I', 'python', 'much']
Reverse elements of ‘b
print(b[::-1])
# ('tutorials', 'me', 'give')
Extract “tuto” as a string from ‘b
print(b[2][:4])
Create a shallow copy of ‘c
print(c[:])
# ['only', 'if', 'good']
Construct word ‘you‘ using ‘a‘ and print string “I love you
you = a[2][1] + a[2][-2] + a[-1][1]
print(" ".join(a[:2] + [you]))
Create [“good”, “if”]
print(c[:-3:-1])

That is it for this tutorial. If you have any questions comment below.

Looking for more Python Tutorials? Take a look at our Python tutorials for dabblers. We also have more Python tutorials for beginners. And while you’re here don’t forget to get the scoop on the latest tech news we’ve got just for you!

Like what you see? Subscribe to our email list and hit us with a like on our Facebook page to get the latest news updates and tutorials straight to your newsfeed!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.