🎁 Give the #1 gift request of 2024... a ZTM membership gift card! 🎁

Beginner's Guide to Indexing in Python (With Code Examples)

Travis Cuzick
Travis Cuzick
hero image

Ever needed to grab a specific file from a list or pull the first letter from a name? It can be annoying if you try to sort through everything manually.

The good news is that Python lets you grab exactly what you need with just one line of code. It’s called indexing, and it makes your work faster and cleaner.

With indexing, you can access data instantly — no loops, no guesswork. Want the last file in a folder? Or maybe you need to pull data from a specific part of a list? Indexing makes it simple.

In this guide, I’ll show you how to master indexing, from grabbing single items to slicing entire chunks of data. By the end, you’ll be able to handle lists, files, and even 2D grids with confidence.

Let’s get started!

Sidenote: If you find any of this confusing, or simply want a deep dive into Python, check out Andrei's Python Coding course taken by 200,000+ people:

learn python in 2025

It’ll take you from an absolute beginner and teach you everything you need to be hired ASAP.

Alternatively, if you're already pretty good at Python and want to build some interesting and useful projects, why not check out my course on Python Automation:

learn python automation in 2025

It'll show you how to automate all of the boring or repetitive tasks in you life - and makes for some pretty stand out portfolio projects!

With that out of the way, let's get into this 5-minute tutorial!

What is indexing in Python and why does it matter?

When you're working with lists, strings, or collections of data, you rarely need everything at once. Usually, you’re after one specific piece — maybe it’s the first file in a folder, the last entry in a system log, or a specific letter from a name.

That’s where indexing comes in. It’s like having a shortcut that takes you straight to the item you want. No searching, no loops — just one simple command.

Here’s a simple way to picture it: Imagine a row of lockers, each with a number on it — 0, 1, 2, 3, and so on.

If you need something from locker 2, you don’t open every locker to find it. You walk straight to locker 2 and grab it. Python works the same way. Each item in a list has a position (called an index), and you can use that position to grab it instantly.

This idea of “position-based access” makes your code cleaner, faster, and easier to read. Instead of searching for an item or writing loops, you can grab what you need right away.

You’ll see this concept pop up constantly when working with lists, text, and even more complex data structures.for working with lists, strings, and even more complex structures like 2D grids and CSV files.

How does indexing work in Python?

When you create a list, Python automatically gives each item a position number — this is called an index. But here’s the part that trips a lot of people up: the first position isn’t 1 — it’s 0.

That might seem odd since humans naturally count from 1, 2, 3, but Python (and most programming languages) count from 0. Why? It’s not just a quirky choice — it actually makes coding simpler and more consistent across programming languages like JavaScript, C, and Java.

Here’s the key idea: When a computer looks at a list, it doesn’t see it as "first, second, third." It sees it in terms of distance from the start.

So:

  • The very first item is already at the start, so it’s at position 0
  • The second item is 1 step away from the start
  • The third item is 2 steps away, and so on

This system might seem odd at first, but it makes certain calculations much easier. If you’ve ever worked with grids, coordinates, or lists in other languages like JavaScript, C, or Java, you’ll see they follow this exact same approach.

Once you get the hang of it, zero-indexing feels natural. And if you’re wondering how to actually access items using these indexes, you’re in luck — that’s exactly what we’ll cover next.

Positive indexing (counting from the start)

When you want to grab items from the start of a list, you use positive indexes like 0, 1, 2, and so on, where each index points directly to an item in the list, just like locker numbers.

For example

Imagine you have a list of filenames:

filenames = ['report.txt', 'data.csv', 'summary.pdf']

Here’s how Python “labels” each item:

  • report.txt is at index 0
  • data.csv is at index 1
  • summary.pdf is at index 2

If you want the first file, you use index 0 like this:

first_file = filenames[0]

If you want the second file, you use index 1:

second_file = filenames[1]

And if you want the third file, you can guess it — index 2.

Negative indexing (counting backward)

So far, we’ve been counting from the front of the list using positive indexes like 0, 1, 2. But what if you want to grab something from the end of the list — like the last file — without worrying about how long the list is?

That’s where negative indexing comes in. Instead of starting at the front, Python lets you count from the back using negative numbers. Here’s how it works:

  • The last item in the list is at index -1.
  • The second-to-last item is at index -2.
  • The third-to-last item is at index -3, and so on.

For example

filenames = ['report.txt', 'data.csv', 'summary.pdf', 'log.txt']

If you want the second-to-last item, you’d do this:

last_file = filenames[-1]

If you want the second-to-last file, you’d do this:

second_last_file = filenames[-2]

This is super useful when you don’t know how long the list will be. No matter how many files get added, filenames[-1] will always give you the last one. This approach is perfect for handling log files, recent updates, or dynamic lists where the length might change.

With both positive and negative indexing in your toolkit, you can access data from either direction. Next, we’ll take it a step further and learn how to grab chunks of a list using something called slicing.

What is Slicing and why does it matter?

Sometimes, grabbing just one item isn’t enough. You might want an entire chunk of a list — like a range of rows from a table or several log entries from a system file. That’s where slicing comes in.

If indexing is like opening one locker from a row, slicing is like grabbing multiple lockers at once. Imagine you want everything from locker 2 to 4. Instead of opening each one individually, you make one clean cut and grab them all at once.

That’s exactly what slicing does. It lets you grab a portion of a list in one simple command. No loops, no extra logic. You just tell Python where to start, where to stop, and (optionally) how many steps to take. It’s quick, clean, and incredibly useful for working with larger datasets.

Next, I’ll show you exactly how slicing works and how you can use it to grab entire sections of lists, filenames, and even rows from a table.

How does Slicing work?

Slicing works by using a special start:stop:step syntax. It tells Python exactly where to begin, where to end, and how many items to skip along the way. Here’s what it looks like:

list[start:stop:step]

Here’s a quick breakdown of what each part means:

  • start: The index where you want to start (this position is included)
  • stop: The index where you want to stop (this position is not included)
  • step (optional): How many items to skip at a time (default is 1, meaning "grab every item")

Here’s a simple way to think about it. If you don’t provide a start, Python assumes you want to start at the beginning. If you don’t provide a stop, it goes all the way to the end. And if you leave out the step, it grabs each item one-by-one.

For example, here’s a list of fruits:

fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']

If you want to grab a portion of this list (let’s say from banana to date), you’d do this:

subset = fruits[1:4]  # Grab from index 1 to 3 (stop before 4)

This gives you:

['banana', 'cherry', 'date']

Notice how it stopped right before index 4. That’s the key thing to remember about slicing — the stop index is not included.

Once you get the hang of it, slicing becomes one of the most powerful tools in Python. Up next, I’ll walk you through positive, negative, and step-based slicing so you can use it like a pro.

Positive slicing

Now that you know how slicing works, let’s see it in action. Positive slicing is when you grab items starting from the front of a list using regular, positive indexes.

Here’s an example list of filenames:

filenames = ['report.txt', 'data.csv', 'summary.pdf', 'log.txt', 'error.log']

If you want to grab the first three files, you’d do this:

first_three_files = filenames[0:3]

Here’s what you get:

['report.txt', 'data.csv', 'summary.pdf']

Notice how Python started at index 0 and stopped before index 3. That’s the "stop-before" rule of slicing.

If you want to grab files starting from the very beginning, Python offers a shorthand syntax. Instead of explicitly writing 0 as the starting index, you can simply skip it:

first_three_files = filenames[:3]

This tells Python, "Start at the beginning and stop before index 3." It’s a cleaner, faster way to do the same thing. Positive slicing is perfect for grabbing chunks of files, rows of data, or any list where you know you want to start from the beginning.

Next, let’s see how you can slice from the end of the list using negative slicing.

Negative slicing

What if you want to grab a chunk of items starting from the end of the list? Just like negative indexing, you can use negative slicing to count from the back of the list.

This is incredibly useful when you want the most recent files, latest log entries, or anything at the end of a list — even if you don’t know how long the list is.

Here’s our list of filenames again:

filenames = ['report.txt', 'data.csv', 'summary.pdf', 'log.txt', 'error.log']

If you want to grab the last three files, you’d do this:

last_three_files = filenames[-3:]

Here’s what you get:

['summary.pdf', 'log.txt', 'error.log']

This tells Python, "Start from the third-to-last file and grab everything to the end." Notice how we didn’t provide a stop value, so Python went all the way to the end of the list.

You can also grab a specific chunk from the middle using negative slicing. If you want to grab data.csv, summary.pdf, and log.txt, you can do this:

middle_files = filenames[-4:-1]

This tells Python, "Start at the fourth-to-last file and stop right before the last one." Here’s what you get:

['data.csv', 'summary.pdf', 'log.txt']

Negative slicing gives you total flexibility, no matter how long the list is. Up next, I’ll show you how to get even more control by using step-based slicing to grab every second, third, or nth item from a list.

Advanced slicing using steps

So far, you’ve seen how to grab chunks of a list using start and stop. But what if you want every other item, every third item, or some other pattern? That’s where the step comes in.

The full slicing syntax looks like this:

list[start:stop:step]

Here’s how it works:

  • start: The index where you want to start (included)
  • stop: The index where you want to stop (not included)
  • step: How many items to skip at a time (default is 1, meaning "grab every item")

Here’s a list of filenames:

filenames = ['report.txt', 'data.csv', 'summary.pdf', 'log.txt', 'error.log']

If you want to grab every other file, you’d do this:

every_other_file = filenames[::2]

Here’s what you get:

['report.txt', 'summary.pdf', 'error.log']

Here’s what’s happening:

  • start is empty, so Python starts at the beginning
  • stop is empty, so it goes all the way to the end
  • step is 2, so it grabs every second item

You can also combine start, stop, and step for even more control. For example, if you only want to grab every other file but only from the middle of the list, you could do this:

range_step_files = filenames[1:4:2]

This tells Python, "Start at index 1, stop before index 4, and grab every second item." Here’s what you get:

['data.csv', 'log.txt']

Using the step value gives you full control over how you pull slices from a list. Up next, we’ll see how this all works with multi-dimensional lists — like rows and columns in a table.

Multi-dimensional indexing and working with nested lists

So far, we’ve been working with single lists, but what if you have lists inside lists? This happens all the time when you’re working with tables, grids, or CSV files. If you’ve used Excel or Google Sheets, it’s basically the same concept — rows and columns.

In Python, you can create a 2D list (a list inside a list) like this:

table = [
	['Name', 'Age', 'Country'],
	['Alice', 25, 'USA'],
	['Bob', 30, 'UK'],
	['Charlie', 22, 'Canada']
]

Here’s how you can think about it:

  • Each row is a list
  • Each column is an item in that row

If you want to grab Bob’s age, you need to access row 2 and column 1. Here’s how you do it:

bobs_age = table[2][1]  # Row 2, Column 1

Here’s what happens step-by-step:

First of all, table[2] grabs the entire row for Bob:

['Bob', 30, 'UK']

Then, table[2][1] grabs the item at index 1 in Bob’s row:

30

Which then gives you Bob’s age of 30.

Cool right? You can even combine indexing and slicing to grab larger chunks of the table.

For example, if you want to grab all the ages, you could do this:

ages = [row[1] for row in table[1:]]

Here’s what this does:

  1. It slices the table from row 1 onward (so it skips the header)
  2. It grabs the item at index 1 (the age) from each row

Here’s the result:

[25, 30, 22]

Multi-dimensional indexing is incredibly useful when working with large datasets, CSV files, or any kind of tabular data. Up next, I’ll cover some common mistakes people run into when working with indexing and how to avoid them.

Common indexing mistakes and how to avoid them

Even though indexing is simple once you get it, there are still a few common mistakes that trip people up. Here’s a breakdown of the most frequent issues — and how to avoid them.

Off-by-one errors

This happens when you forget that Python starts counting from 0, not 1. It’s one of the most common mistakes for new coders.

The mistake:

filenames = ['report.txt', 'data.csv', 'summary.pdf']
second_file = filenames[2]  # ❌ Wrong! This points to 'summary.pdf'

What went wrong? You wanted the second file (data.csv), but since Python starts at 0, index 2 points to the third item.

The fix:

Use index 1 for the second item.

second_file = filenames[1]  # ✅ Correct!

If you’re ever unsure, just count it out:

  • Index 0 = 'report.txt'
  • Index 1 = 'data.csv'
  • Index 2 = 'summary.pdf'

Index out of range

If you try to access an index that doesn’t exist, Python will throw this error:

IndexError: list index out of range

The mistake:

filenames = ['report.txt', 'data.csv', 'summary.pdf']
file_too_far_back = filenames[-10]  # ❌ This will throw an IndexError

Here, you’re trying to access an index that doesn’t exist. The list only has 3 items, so there is no -10 position.

The fix:

If you’re not sure how long the list is, you can check its length like this:

index = -10
if abs(index) <= len(filenames):
	print(filenames[index])
else:
	print("Index out of range!")

This way, you won’t get hit with an error. You can also avoid this issue by sticking to [-1], [-2], [-3], which are safe for shorter lists.

Mixing up positive and negative indexes

It’s easy to confuse positive and negative indexes since they both "count" in different directions.

The mistake:

filenames = ['report.txt', 'data.csv', 'summary.pdf']
file = filenames[-2]  # Correct! Points to 'data.csv'
file = filenames[2]   # Also correct! Points to 'summary.pdf'

Notice how -2 and 2 both grab items, but from opposite directions. It’s easy to accidentally mix them up.

The fix:

Remember this rule:

  • Positive indexes count forward from 0
  • Negative indexes count backward from -1

If you’re ever unsure, visualize it like this:

Index 0 1 2
File report.txt data.csv summary.pdf
Negative -3 -2 -1

With this mental image, it’s easy to see how 2 and -1 point to completely different items.

These are the most common mistakes people run into when working with indexing and slicing. If you can avoid these, you’ll save yourself a lot of debugging time. Next, I’ll wrap things up with a quick review of everything you’ve learned.

Now it’s your turn!

You’ve just unlocked one of Python’s most essential skills: indexing and slicing. No more counting items by hand or writing loops to grab values. Now, you can access and slice your data with just one line of code.

Here’s a quick recap:

  • Indexing: Grab specific items from lists, strings, and collections
  • Slicing: Pull multiple items at once with [start:stop:step]
  • Multi-dimensional indexing: Work with tables, grids, and lists inside lists

These skills are everywhere in Python — from file handling to automation scripts to working with CSV files.

Now it’s your turn. Try it out! Write a script that grabs specific items, slices a range, or works with a 2D table. The more you practice, the more natural it will feel.

P.S.

Remember - If you want to dive deep into Python then be sure to check out Andrei's Complete Python Developer course:

learn python in 2025

It’ll take you from an absolute beginner and teach you everything you need to get hired ASAP and ace the tech interview.

This is the only Python course you need if you want to go from complete Python beginner to getting hired as a Python Developer this year!

Alternatively, if you're already pretty good at Python and want to build some interesting and useful projects, why not check out my course on Python Automation:

learn python automation in 2025

It'll show you how to automate all of the boring or repetitive tasks in you life - and makes for some pretty stand out portfolio projects!

Plus, as part of your membership, you'll get access to both of these courses and others, and be able to join me and 1,000s of other people (some who are alumni mentors and others who are taking the same courses that you will be) in the ZTM Discord.


Ask questions, help others, or just network with other Python Developers, students, and tech professionals.

More Beginner 5-Minute Python Tutorials

If you enjoyed this post, check out my other Python tutorials:

More from Zero To Mastery

Beginner’s Guide To Python Automation Scripts (With Code Examples) preview
Beginner’s Guide To Python Automation Scripts (With Code Examples)

Use Python automation scripts to save time, reduce errors, and boost productivity. Perfect for all skill levels. Work smarter, not harder!

How To Become A Web Developer (From Complete Beginner to Hired) preview
How To Become A Web Developer (From Complete Beginner to Hired)

Want to become a Web Developer but not sure how? Our step-by-step guide shows you how to go from beginner to hired (without wasting $1,000s on a bootcamp or degree). And we answer all your FAQ.

Potential Python Interview Questions preview
Potential Python Interview Questions

Are you taking a coding interview with Python? On rare occasions, you may be asked broad language questions before the technical interview: Here's 25 of them.