[Python Tips] Slices

in #programming7 years ago

This isn't as much of a tip but a walkthrough on how to use slices and all the cool things you can do with them. Slices are a very powerful feature available to Python developers that are worth understanding and taking advantage of.

Python Slices

Slices are a way to pull a sequence from a list with a simple yet powerful syntax. Have a list of one hundred objects and just want the first 10? last 10? every other one? Slices have you covered!

Slices syntax is simple but can be very confusing, but are super powerful and should not be ignored.

For all these examples we are going to use the list below as our dataset.

numbers = list(range(1, 101)

This results in a list that looks like this:

numbers [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 5 4, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

This is perfect for showing off slices.

Slices use the : to represent start, end, and step for pulling a sequence from a list and look like this [::] instead of [1] when pulling an element from a list.

Simple range

Let's say you want the first 10 numbers from the list. You can easily just do this.

>>> numbers[0:10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Last 10 numbers?

>>> numbers [-10:]
[91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

Now, this may look confusing if you haven't used slices before and there is something to remember as it doesn't appear consistent.

From above, I mention the three parameters to a slice is `start', 'end', 'step' where they are all optional.

In our first example numbers[0:10] we are asking for the first element to the 10th element. This may look weird as the start is zero based but the end is n+1. It is confusing at first but once you get used to it, it isn't so bad. Think of it more like how many elements you want and not x position.

The second example numbers [-10:] may be confusing, but the - symbol simply means in reverse. So here we are saying start from the 10th spot but working backward. So instead of starting at the 10th element, we are going to start 10 elements from the end. You can see our end location was omitted. This means to run until the end. So in this example, we have asked for all elements in the list until the end starting 10 before the end.

Even numbers

Let's say we want all even numbers, we can do that using the step parameter to get every other element until the end.

Before you look at the example, take a guess how you would do this? This one is hard at first but once you understand it, it is easy.

>>> numbers[1::2]
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]

In this example, we started at the second element (remember start is zero-based), and we want to go to the end so I omitted the end parameter but I did specify 2 for our step. This will skip every other element or do 2 steps for each increment.

Let's get all elements but the last 10, how would you do it?

>>> numbers[:-10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 5
4, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90]

Making sense? Can you see the power and efficiency?

Make a new list

Slices can be used to make new lists as well, this is really powerful for making subsets easily.

Let's take our even numbers example above, and make a separate list.

>>> even_numbers = numbers[1::2]
>>> even_numbers
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]
>>> numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 5
4, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

As you can see, our base list numbers remains untouched. This is great if you want to make a sub-list of another list. Keep in mind this is a copy and if you change numbers the new list even_numbers will not change. There are other ways to do even numbers and make sub-lists as well. This use case isn't the best tool for every job, but it is useful when needed.

Strings

You can also use slice notation with strings!

>>> name = 'hello world'
>>> name[0:5]
'hello'
>>> name = 'hello world'
>>> name[-5:]
'world'

Tuples

Slicing is supported on Tuples as well.

>>> mytuple = (1, 2, 3, 4, 5)
>>> mytuple[0:2]
(1, 2)

That's it for this session, hopefully, you learned something or can appreciate the power of slices that you haven't been using effectively.

Leave a comment if you found it helpful or thought it sucked. I'd love to know if anyone is finding this helpful or if I'm just posting into a blackhole.

My Python Tips Series

X48EJ

Why you should vote me as witness

Witness & Administrator of four full nodes

themarkymark.png

My recent popular posts

STEEM, STEEM Power, Vests, and Steem Dollars. wtf is this shit?
The truth and lies about 25% curation, why what you know is FAKE NEWS
WTF is a hardware wallet, and why should you have one?
GINABOT - The Secret to your Sanity on Steemit
How to calculate post rewards
Use SSH all the time? Time for a big boy SSH Client
How to change your recovery account
How curation rewards work and how to be a kick ass curator
Markdown 101 - How to make kick ass posts on Steemit
Work ON your business, not in your business! - How to succeed as a small business
You are not entitled to an audience, you need to earn it!
How to properly setup SSH Key Authentication - If you are logging into your server with root, you are doing it wrong!
Building a Portable Game Console

Sort:  

This is great! Thanks for sharing!

Another example of the power of slices can be string reverse,

>>> "jamzed"[::-1]
'dezmaj'

Good example.

I've been trying to delve into the world of Python but keep getting sidetracked with projects before getting too deep. This is an excellent tutorial on slices though. Thank you.

whether it is the formula of a python
U5drMmxzAadtcV72443uHhBGqXaorvn.gif

amazing information, I love to read post on this topic, very good your post has my vote!

Loading...