#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
6 min read min read

Lists Basics

Learn how to store and work with multiple items using Python lists

Lists Basics

What is a List?

Think of a list like a shopping list. Instead of storing one item, you can store many items together in order.

A list in Python is a collection that holds multiple values. You can put numbers, text, or even mix different types of data in one list.

Why use lists?

  • Store related items together (like names of students, prices of products)
  • Keep data organized in order
  • Easy to add, remove, or change items
  • Work with many values at once

Creating a List

You create a list using square brackets. Put your items inside, separated by commas.

code.py
fruits = ["apple", "banana", "orange"]
print(fruits)

What happens here: Python creates a list with 3 fruit names. The order matters - apple is first, banana is second, orange is third.

Different types of lists:

  1. List of numbers - Store ages, prices, scores
code.py
ages = [25, 30, 18, 42]
  1. List of text - Store names, cities, colors
code.py
colors = ["red", "blue", "green"]
  1. Mixed list - Combine different types (not common but possible)
code.py
mixed = ["John", 25, True]
  1. Empty list - Start with nothing, add items later
code.py
empty = []

Accessing Items in a List

Each item in a list has a position number called an index. Python starts counting from 0, not 1.

code.py
fruits = ["apple", "banana", "orange"]
print(fruits[0])
print(fruits[1])

What this does:

  • fruits[0] gives you "apple" (first item)
  • fruits[1] gives you "banana" (second item)

Important: The first item is at position 0, second item at position 1, and so on.

Negative Indexing

You can count from the end using negative numbers. This is helpful when you want the last item.

code.py
fruits = ["apple", "banana", "orange", "grape"]
print(fruits[-1])
print(fruits[-2])

What happens:

  • fruits[-1] gives you "grape" (last item)
  • fruits[-2] gives you "orange" (second from end)

Changing List Items

Lists are flexible. You can change any item by accessing its position and giving it a new value.

code.py
fruits = ["apple", "banana", "orange"]
fruits[1] = "mango"
print(fruits)

What this does: Takes the second item (banana) and replaces it with "mango". Now the list is ["apple", "mango", "orange"].

Checking List Length

Use the len() function to count how many items are in your list.

code.py
fruits = ["apple", "banana", "orange"]
count = len(fruits)
print(count)

Result: Shows 3 because there are 3 items in the list.

Checking if Item Exists

Use the word "in" to check if something is in your list. This gives you True or False.

code.py
fruits = ["apple", "banana", "orange"]
print("apple" in fruits)
print("grape" in fruits)

What happens:

  • First line prints True (apple is in the list)
  • Second line prints False (grape is not in the list)

Practice Example

The scenario: You're managing a small library. You have 4 books and want to check information about them.

code.py
books = ["Python Basics", "Data Science", "Web Design", "AI Guide"]

print("Total books:", len(books))
print("First book:", books[0])
print("Last book:", books[-1])

books[2] = "JavaScript Guide"
print("Updated list:", books)

if "Python Basics" in books:
    print("Python book available")

What this program does:

  1. Creates a list of 4 book names
  2. Counts total books (shows 4)
  3. Shows first book (Python Basics)
  4. Shows last book (AI Guide)
  5. Changes third book from Web Design to JavaScript Guide
  6. Checks if Python Basics is available (it is, so prints message)

Key Points to Remember

Lists are containers that hold multiple items in order. You create them with square brackets and separate items with commas.

Python counts positions starting from 0, not 1. The first item is at index 0, second at index 1, and so on.

You can count from the end using negative numbers. The last item is -1, second from end is -2.

Lists can be changed after creation. You can update any item by accessing its position and assigning a new value.

Use len() to count items and "in" to check if something exists in the list.

Common Mistakes

Mistake 1: Forgetting Python starts at 0

code.py
numbers = [10, 20, 30]
print(numbers[1])  # This gives 20, not 10

Mistake 2: Trying to access position that doesn't exist

code.py
fruits = ["apple", "banana"]
print(fruits[5])  # Error! Only 2 items exist

Mistake 3: Forgetting square brackets when creating list

code.py
colors = "red", "blue"  # This creates tuple, not list
colors = ["red", "blue"]  # Correct way

What's Next?

Now you know list basics. In the next lesson, you'll learn advanced list operations like adding items, removing items, sorting, and more ways to work with lists.

SkillsetMaster - AI, Web Development & Data Analytics Courses