Python Tutorial

Introduction

Edit Template
  • Home
  • /
  • Python List Indexes

Python Tutorial

Introduction

Edit Template
  • Home
  • /
  • Python List Indexes

Python List Indexes

When you create a list in Python, every item inside it gets its own unique index number. This helps you access or work with specific items easily.

Think of a list like a shelf. Each book (item) has its own position (index), and you can pick one just by pointing to its place.

Accessing Items Using Positive Indexes

Python indexes start from 0. So the first item is at index 0, the second is at 1, and so on.

				
					fruits = ["Apple", "Mango", "Banana", "Orange", "Peach"]

print(fruits[0])  # First item
print(fruits[2])  # Third item
print(fruits[4])  # Fifth item

				
			

Output:

				
					Apple
Banana
Peach

				
			

Accessing Items Using Negative Indexes

Negative indexes let you start counting from the end of the list. -1 is the last item, -2 is second-last, and so on.

				
					fruits = ["Apple", "Mango", "Banana", "Orange", "Peach"]

print(fruits[-1])  # Last item
print(fruits[-3])  # Third from end
print(fruits[-5])  # First item

				
			

Output:

				
					Peach
Banana
Apple

				
			

Checking if an Item Exists

You can check whether a certain item exists in the list using the in keyword.

				
					colors = ["Red", "Blue", "Green", "Yellow"]

if "Green" in colors:
    print("Green is in the list.")

if "Purple" not in colors:
    print("Purple is not in the list.")

				
			

Output:

				
					Green is in the list.
Purple is not in the list.

				
			

Slicing: Accessing a Range of Items

You can extract multiple items by slicing the list using:

				
					list[start : end : step]

				
			

Note: start is included, end is not. Step is optional.

Example 1: Slice Using Positive Indexes
				
					animals = ["Lion", "Tiger", "Elephant", "Zebra", "Deer", "Wolf", "Bear"]

print(animals[1:5])

				
			

Output:

				
					['Tiger', 'Elephant', 'Zebra', 'Deer']

				
			
Example 2: Slice Using Negative Indexes
				
					print(animals[-6:-2])

				
			

Output:

				
					['Tiger', 'Elephant', 'Zebra', 'Deer']

				
			
Example 3: From Middle to End
				
					print(animals[3:])

				
			

Output:

				
					['Zebra', 'Deer', 'Wolf', 'Bear']

				
			
Example 4: From Start to Middle
				
					print(animals[:4])

				
			

Output:

				
					['Lion', 'Tiger', 'Elephant', 'Zebra']

				
			

Skipping Items With Step

You can skip elements by giving a step size.

				
					print(animals[::2])  # Every second item

				
			

Output:

				
					['Lion', 'Elephant', 'Deer', 'Bear']

				
			

Custom Step With Range

Let’s pick every 3rd item from a portion of the list:

				
					print(animals[1:7:3])

				
			

Output:

				
					['Tiger', 'Deer']

				
			

Summary

  • Indexes start from 0 (or -1 from the end).

  • Slicing lets you access a range of items.

  • Step lets you skip through the list.

  • Use in to check if an item exists.

This is the foundation of working with data in Python lists — simple, fast, and flexible. Let’s move forward to updating and modifying these items!

Scroll to Top