← Back to Chapters

Python Access List Items

? Python Access List Items

⚡ Quick Overview

In Python, lists store multiple values in a single variable. To work with these values, you need to know how to access individual items, ranges of items, and how to check if a value exists in the list.

You can:

  • Use indexes (starting from 0) to access specific items.
  • Use negative indexes to access items from the end.
  • Use slicing to access a range of items.
  • Use the in keyword to check if an item exists in the list.

? Key Concepts

  • Positive Indexing: Starts from 0 for the first item.
  • Negative Indexing: Starts from -1 for the last item.
  • Slicing: Accesses a sublist using list[start:end].
  • Membership Check: Uses in to check if an item exists.

? Syntax & Theory

? Indexing

list_name[index] – returns the item at the given index.

? Negative Indexing

list_name[-1] – last item, list_name[-2] – second last, and so on.

? Slicing

list_name[start:end] – returns items from start index up to (but not including) end index.

? Membership

item in list_name – returns True if the item exists in the list.

? Code Examples

? Accessing Items by Index
fruits = ["apple", "banana", "cherry"]
print(fruits[0])   # apple
print(fruits[2])   # cherry
? Using Negative Indexing
fruits = ["apple", "banana", "cherry"]
print(fruits[-1])   # cherry
print(fruits[-2])   # banana
✂️ Range of Indexes (Slicing)
numbers = [10, 20, 30, 40, 50, 60]
print(numbers[1:4])   # [20, 30, 40]
print(numbers[:3])    # [10, 20, 30]
print(numbers[3:])    # [40, 50, 60]
? Check if Item Exists
fruits = ["apple", "banana", "cherry"]
if "banana" in fruits:
    print("Yes, banana is in the list!")

? Live Output & Explanation

?️ Expected Outputs

  • fruits[0]"apple" (first item).
  • fruits[2]"cherry" (third item).
  • fruits[-1]"cherry" (last item).
  • fruits[-2]"banana" (second last item).
  • numbers[1:4][20, 30, 40] (indexes 1, 2, 3).
  • numbers[:3][10, 20, 30] (start to index 2).
  • numbers[3:][40, 50, 60] (index 3 to end).
  • "banana" in fruitsTrue, so the message is printed.

Notice how slicing always stops before the end index. Also, negative indexes are very handy when you only care about items near the end of the list.

? Tips & Best Practices

  • Use negative indexing to quickly get items from the end of a list.
  • Use slicing to extract sublists without modifying the original list.
  • The in operator is great for quick membership checks.
  • Remember that Python uses zero-based indexing: the first item is at index 0.
  • Use meaningful list names (like fruits, numbers) for readability.

? Try It Yourself

  • Create a list of animals and access the third item using its index.
  • Use negative indexing to print the last two items of a list.
  • Slice a list of numbers to get only the middle values.
  • Write a condition to check if "mango" is in a list of fruits.
  • Experiment with different start and end values in slicing to see how the result changes.

? When to Use List Access

  • Processing specific elements in data (e.g., first/last record in a list).
  • Extracting a portion of data (e.g., first 10 users, last 5 scores).
  • Checking if a value exists before performing an operation on it.