Jovian
Sign In
Learn practical skills, build real-world projects, and advance your career

Python Indexing and Slicing

Python Indexing and Slicing

Python provides four basic inbuilt data structures that are: List, Tuple, Dictionary, and Set. To iterate through them, we use skills called Slicing and Indexing. Before getting into these, we need to understand the concept of an Index.

Index

Python assigns each element with a label called "index". This index value can be used to access elements within an interable data structure.

Just how in squid games, each player was labeled as a number and this number basically became their identification.

In modern programming languages, each index value begins with zero. i.e \(0,1,2,3...\) and so on.

Indexing

Indexing refers to an element's position in an iterable data type. In simple words, indexing is used to access an element using its index value.

You can directly call an element by stating their index value:

a_list[index_number]

Additionally, to find out the index value of an element, we can use an inbuilt function called .index('element').

a_list = [5,4,6,'a']

for element in a_list:
  index_value = a_list.index(element)

  print("Index value: ", index_value,", List Element", element)
Index value: 0 , List Element 5 Index value: 1 , List Element 4 Index value: 2 , List Element 6 Index value: 3 , List Element a

Imgur

Negative Indexing

You can also traverse a list from the end using negative indexing. [-1] returns the last element of the list and moves towards the first element in the format: \(... -3, -2, -1\). This way the negative index of the first element is -(length_of_list).

a_list = ['r','e','m','o','t','e']

a_list[-1]
'e'
n = len(a_list)
a_list[-n]
'r'

Slicing

Slicing refers to selecting a subset of an iterable data type. This can be used to select a certain range or elements from your lists.

The syntax for slicing looks like:

a_list[starting_index:stopping_index]

Imgur

The sliced list begins from starting_index and ends at (stopping_index -1). i.e, in the example below, the slice[3:8] will count the element at 3rd index but not include the element at 8th index.

a_list = ['m','e','m','o','r','y',1, 0, 1]

sample = a_list[3:8]
sample
['o', 'r', 'y', 1, 0]

Subsetting first or last 'n' elements:

When we want to select first 'n' elements, we can skip the starting_index and directly call the slicing notation. This will start our slice from zero index untill the starting_index.

a_list = ['m','e','m','o','r','y',1, 0, 1]

a_list[:5]
['m', 'e', 'm', 'o', 'r']

Simillarly, for choosing the last 'n' elements we can use negative indexing before the colon.

a_list = ['m','e','m','o','r','y',1, 0, 1]

a_list[-5:]
['r', 'y', 1, 0, 1]

Imgur

Stepping

Another interesting way of slicing your lists is to give an interval for selecting or ignoring certain elements in the slice.

a_list = ['m','e','m','o','r','y',1, 0, 1]

a_list[1:len(a_list):2]
['e', 'o', 'y', 0]

The above can also be written like:

a_list = ['m','e','m','o','r','y',1, 0, 1]

a_list[1::2]
['e', 'o', 'y', 0]

Conclusion

Indexing and Slicing are Analogical concepts that go hand in hand. Playing around with them, always gives a better idea of what kind of results indexing and slicing an array provide. Its better to try out various arguments and check for yourself.

The same indexing convention is used in other python tools like Numpy arrays and Pandas DataFrames or Series. This makes it very important to get the basic hold of these skills for future application.

# Execute this to save new versions of the notebook
jovian.commit(project="indexing-slicing")
[jovian] Detected Colab notebook... [jovian] Uploading colab notebook to Jovian... Committed successfully! https://jovian.ai/himani007/indexing-slicing
 
himani007
Himani8 months ago