Monday, October 19, 2020

Stack Abuse: Python: Slice Notation on List

Introduction

The term slicing in programming usually refers to obtaining a substring, sub-tuple, or sublist from a string, tuple, or list respectively.

Python offers an array of straightforward ways to slice not only these three but any iterable. An iterable is, as the name suggests, any object that can be iterated over.

In this article, we'll go over everything you need to know about Slicing Lists in Python.

Slicing a List in Python

There are a couple of ways to slice a list, most common of which is by using the : operator with the following syntax:

a_list[start:end]
a_list[start:end:step]

The start parameter represents the starting index, end is the ending index, and step is the number of items that are "stepped" over.

If step isn't explicitly given, the default value is 1. Note that the item with the index start will be included in the resulting sublist, but the item with the index end won't be. The first element of a list has the index of 0.

Example without the step parameter:

# A list of strings:
a_list = ['May', 'the', 'Force', 'be', 'with', 'you.']
sublist = a_list[1:3]
print(sublist)

This should print:

['the', 'Force']

To skip every other word, set step to 2:

a_list = ['The', 'Force', 'will', 'be', 'with', 'you.', 'Always.']
sublist = a_list[1:8:2]
print(sublist)

Output:

['Force', 'be', 'you.']

If step isn't listed, the sublist will start from the beginning. Likewise, if end isn't listed, the sublist will end at the ending of the original list:

a_list = ['Do.', 'Or', 'do', 'not.', 'There', 'is', 'no', 'try.']
sublist = a_list[:4]
print(sublist)
sublist = a_list[4:]
print(sublist)

That snippet of code prints out:

['Do.', 'Or', 'do', 'not.']
['There', 'is', 'no', 'try.']

Finding the Head and Tail of List with Slice Notation

The slice notation can be used with negative indexing as well. Negative indexing works the same way as regular indexing, except for the fact that it starts indexing from the last element which has the index -1.

This can be used to obtain the head and tail of a list of a given length. The head of a list is a sublist that contains the first n elements of a list, and the tail is a sublist that contains the last n elements.

Let's go ahead and separate a tail and head of a list:

# The length of the tail
n = 2
a_list = ['Never', 'tell', 'me', 'the', 'odds!']

# Head of the list:
sublist = a_list[:n]
print(sublist)

# Tail of the list:
sublist = a_list[-n:]
print(sublist)

This outputs:

['Never', 'tell']
['the', 'odds!']

Using Slice Notation to Reverse a List

Even the step parameter can be negative. If we set it to a negative value, the resulting list will be reversed, with the step value. Instead of stepping forward, we're stepping backwards, from the end of the list to the start and including these elements:

a_list = ['Power!', 'Unlimited', 'power!']
sublist = a_list[::-1]
print(sublist)

This results in:

['power!', 'Unlimited', 'Power!']

Replacing Elements of a Sublist with Slice Notation

The slice notation can be used to asign new values to elements of a certain sublist. For example, let try to replace the tail and the head of a list:

a_list = ['I', 'am', 'no', 'Jedi.']
print(a_list)
# Replacing the head of a list
a_list[:1] = ['You', 'are']
print(a_list)
# Replacing the tail of a list
a_list[-1:] = ['Sith']
print(a_list)

The expected output is:

['I', 'am', 'no', 'Jedi.']
['You', 'are', 'no', 'Jedi.']
['You', 'are', 'no', 'Sith']

Replacing Every n-th Element of a List with Slice Notation

An easy way to replace every n-th element of a list is to set the step parameter to n in the slicing notation:

 a_list = ['I’m', 'just', 'a', 'simple', 'man', 'trying', 'to', 'make', 'my', 'way', 'in', 'the', 'universe.']
    
print(a_list)

# Replacing every other word starting with the word with the index 1
a_list[1::2] = ['only', 'common', 'attempting','do', 'best','the']
print(a_list)

This results in:

['I’m', 'just', 'a', 'simple', 'man', 'trying', 'to', 'make', 'my', 'way', 'in', 'the', 'universe.']
['just', 'simple', 'trying', 'make', 'way', 'the']
['I’m', 'only', 'a', 'common', 'man', 'attempting', 'to', 'do', 'my', 'best', 'in', 'the', 'universe.']

Conclusion

Slicing any sequence in Python is easy, simple, and intuitive. Negative indexing offers an easy way to acquire the first or last few elements of a sequence, or reverse its order.

In this article, we've covered how to apply the Slice Notation on Lists in Python.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...