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 and a. Let’s see how we can do that.
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.
Example: let’s take the list a, we want to get the last element of it.
Above code will print “much“. Now let’s try to get the second last element.
This will print “very“.
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.
|tuple(<sequence>)||This function can create a tuple from a sequence.
d = tuple(a) # ('I', 'love', 'python', 'very', 'much') d = tuple() # ()
|list(<sequence>)||This function can create a list from a sequence.
d = list(b) # ['give', 'me', 'tutorials'] d = list() # 
|<str>.join(<string sequence>)||Join a sequence of strings using another string.
" ".join(a) # I love python very much
|len(<sequence>)||Get the length of a sequence.
len(a) # Returns 5
|+ Operator||Join 2 sequences of a same type to a single sequence.
d = a + b # ['I', 'love', 'python', 'very', 'much', # 'only', 'if', 'good']
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‘|
|Create a shallow copy of ‘c‘|
print(c[:]) # ['only', 'if', 'good']
|Construct word ‘you‘ using ‘a‘ and print string “I love you“|
you = a + a[-2] + a[-1] print(" ".join(a[:2] + [you]))
|Create [“good”, “if”]|
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!