Sitemap

Indexing in Python

2 min readNov 30, 2022

--

Looking into it in our thread today is NOT python datatype but the key feature we use on a datatype to achieve predetermined objectives.

You can access a single item of a Python sequence (such as a string, tuple, or list) with the corresponding integer index.

Indexing in Python is zero-based. This means the index 0 corresponds to the first (leftmost) item of a collection, 1 to the second item, and so on.

Python index is positive when you start your indexing from the first left items of say a list.

Examples.

Commenters =[1, 2, 3, 4, 5] Python List

[0, 1, 2, 3, 4] Positive Indexing

Get Habeeb Abdulrauf’s stories in your inbox

Join Medium for free to get updates from this writer.

Engagers =[ 1, 2, 3, 4, 5] Python List

[- 5, — 4, — 3, — 2, — 1] Negative Indexing

Let’s go practical. Say i want to replace 1 in Commenters with some other values. I can simple do that by using indexing to call the number and reassign other values to it thus.

Input

Commenters [0] = [ ‘Monday’, ‘Tuesday’, ‘ Wednesday’, ‘Thursday’, ‘Friday’]

Output

Commenters = [ [ ‘Monday’, ‘Tuesday’, ‘ Wednesday’, ‘Thursday’, ‘Friday’] , 2, 3, 4, 5]

You can use indexing to modify the items of the mutable sequence such as list as I have done above.

#DataAnalytics #pythondeveloper #pythoncode #pythonindex #datavisualization #datascience

--

--

Habeeb Abdulrauf
Habeeb Abdulrauf

Written by Habeeb Abdulrauf

Python l Data Analytics l Web Solution l Curious Learner l Digital Marketing Consultant l SEO Expert l SDGs. My website : https://habeebabdulrauf.com