Understanding Python Dictionary Slicing – Detailed Tutorial with Examples and Problems

In this Tutorial we are going to discuses and understand Python Dictionary Slicing with examples and Problems, And all the possible Ways of slicing a list or dictionary.

Understanding Slicing

Before Understanding slicing, we need to know the indexing in a list and how it works. So indexing is a way to provide a local address to an element stored in a list, As in a list we store n number of elements then how we will identify them, like where that particular element is stored.Slicing Now let’s talk about slicing

What is Slicing

As we know that indexing helps us identify each element uniquely so that we can edit, delete, add, or hide the element means accessing the elements directly. Slicing is a tool for manipulating the elements at a specific index to understand let’s create a list

nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]

we know that we can easy access any one of the indexes and display but if we need to access multiple elements from the list then we can not write each index individually if the list is really large in that case slicing comes into the picture. Let us need elements from index 3 to 7 then we can simply write

ex1 = nums[2:7]
ex1

it will print all the elements from index 2 to 7. here we do not need to write all the indies individually to print all.

ex1 = nums[2:7] here 2 is the starting index and 7 is the ending index.

Types Of slicing
1 Taking first n elements
nums[:5]

will print the first 5 elements of the list

2. Last n elements
nums[-3:]

it will print the last 3 elements from the list.

3. between two Specific indexes
nums[-5:-1]

it will print -1(from last index to index -5 ) that is last 5 elements.

4. Printing all except the last n n elements
nums[:-2]

it will leave the last 2 elements and print the rest of the elements.

5. taking every nth element
nums[::2]

It will print all the elements after one index for example it will print 0,2,4,6,8…. and so on elements as it is given as 2. It is also called hopping.

6. Taking a negative step and Reversing List
nums[::-1]

It will print all the elements in reverse order as the hopping is -1 so every element in a reverse manner.

Slicing Objects

We can create a slicing object to perform the same slicing time and again n number of times. It helps in refusing the code length as many a time we need the first character of the name every time and has to write the code for getting the first character of the first or last name to avoid such a situation we can use slicing object to reduce the length and time os compiler.

five_items_after_second = slice(2, 2 + 5)
nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]
colors = ['red', 'green', 'blue', 'yellow', 'white', 'black', 'silver']
nums[five_items_after_second]
[30, 40, 50, 60, 70]
colors[five_items_after_second]
['blue', 'yellow', 'white', 'black', 'silver']

Here we have two lists and need to perform the same slicing on both we can do this in this manner as once we declared the object of slicing and letter we just used it for performing the same operation on two different lists.

Substitute/ Edite the elements of a list

We can edit multiple elements of the list elements in one go using slicing just like displaying we can also edit them for example

nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]
nums[:4] = [1,2,3,4]
nums
[1, 2, 3, 4, 50, 60, 70, 80, 90]

Here we edited the first 4 elements of the list and gave the values to rewrite.

Note-: this function will edit only those which are being provided in the squared bracket to rewrite if they provide 3 elements instant of 3 to will rewrite only 3 the 4th one will be the same as before
Slicing Deletion

We can also delete the sliced elements just like displaying

nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]
del nums[3:7]
nums

It will delete all the indexes from 3 to 7 and print the rest of the elements. The output of the given code would be
[10, 20, 30, 80, 90] as it will delete all the elements in between indexes of 3 to 7,

So we can conclude that slicing is a tool to deal with the manipulation of the elements using their indexes.

Learn More about slicingĀ https://stackoverflow.com/questions/509211/understanding-slicing

List and their oprations

Leave a Comment

%d bloggers like this: