Lecture

Lists – Creation and Indexing

In Python, a list is a collection of items that are ordered and changeable. Lists are one of the most common data structures used in data analysis.

You can store anything in a list — numbers, strings, or even other lists.


Creating a List

To create a list, use square brackets [] and separate the items with commas:

fruits = ["apple", "banana", "cherry"]

This list has 3 items. Lists can grow or shrink dynamically, and you can update them anytime.


Accessing List Items

Python lists are zero-indexed. This means:

  • The first item is at index 0
  • The second item is at index 1
  • And so on...

You can also use negative indexing to access items from the end:

  • -1 refers to the last item
  • -2 refers to the second-to-last item

Let’s look at some real examples in the code block below.


Code Example

# Creating a list of cities cities = ["New York", "Chicago", "Los Angeles", "Houston"] # Accessing the first item print("First city:", cities[0]) # New York # Accessing the third item print("Third city:", cities[2]) # Los Angeles # Accessing the last item using negative index print("Last city:", cities[-1]) # Houston # Accessing the second-to-last item print("Second-to-last city:", cities[-2]) # Los Angeles

Why This Matters

Lists allow you to store and organize data in a flexible way. Indexing is essential when retrieving specific values for analysis, filtering, or looping through data.

You’ll use lists a lot when analyzing data in Python — get comfortable with them early!


What’s Next?

In the next lesson, you’ll learn how to modify and slice lists, letting you extract groups of values or change parts of the list easily.

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result