Python Lists – The Ultimate Beginner's Guide

Last updated 5 months, 1 week ago | 526 views 75     5

Tags:- Python

A list in Python is a versatile and widely used data structure. It allows you to store multiple items in a single variable, even with mixed data types.

In this tutorial, you’ll learn:

  • What a list is

  • How to create and access list elements

  • Common list operations

  • List methods

  • Nested lists

  • Tips and common pitfalls


What Is a List?

A list is a collection that is:

  • Ordered

  • Mutable (can be changed)

  • Allows duplicate values

Creating a List

fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = ["hello", 42, 3.14, True]

Accessing List Elements

List elements are zero-indexed (the first element is at index 0).

fruits = ["apple", "banana", "cherry"]
print(fruits[0])  # apple
print(fruits[-1]) # cherry (last element)

✏️ Modifying a List

✅ Changing Items

fruits[1] = "blueberry"
print(fruits)  # ['apple', 'blueberry', 'cherry']

✅ Adding Items

  • append() adds to the end:

    fruits.append("orange")
    
  • insert() adds at a specific position:

    fruits.insert(1, "grape")
    

✅ Removing Items

  • remove() deletes a specific item:

    fruits.remove("banana")
    
  • pop() removes by index (default is last):

    fruits.pop()
    
  • del deletes by index:

    del fruits[0]
    
  • clear() empties the list:

    fruits.clear()
    

Looping Through Lists

for fruit in fruits:
    print(fruit)

With index:

for i in range(len(fruits)):
    print(i, fruits[i])

Useful List Methods

Method Description Example
append() Adds to end fruits.append("kiwi")
insert() Adds at index fruits.insert(1, "lemon")
remove() Removes item fruits.remove("apple")
pop() Removes item at index fruits.pop(1)
clear() Empties the list fruits.clear()
index() Finds index of item fruits.index("banana")
count() Counts item appearances fruits.count("apple")
reverse() Reverses list fruits.reverse()
sort() Sorts ascending numbers.sort()
copy() Creates copy new_list = fruits.copy()

Slicing Lists

Extract a portion of the list:

fruits = ["apple", "banana", "cherry", "orange", "kiwi"]
print(fruits[1:4])  # ['banana', 'cherry', 'orange']
print(fruits[:3])   # ['apple', 'banana', 'cherry']
print(fruits[-2:])  # ['orange', 'kiwi']

Nested Lists

Lists within lists:

matrix = [
  [1, 2, 3],
  [4, 5, 6]
]

print(matrix[0][1])  # 2

Example: Grocery List Program

groceries = []

while True:
    item = input("Add item (or 'done'): ")
    if item == 'done':
        break
    groceries.append(item)

print("Your grocery list:")
for item in groceries:
    print("- " + item)

Tips for Working with Lists

  • Use copy() to clone a list instead of =, which shares references.

  • Use list comprehensions for quick transformations:

    squares = [x**2 for x in range(5)]
    
  • Lists can hold any type of object, including other lists and functions.


⚠️ Common Pitfalls

Pitfall Why it happens How to fix it
Using = instead of copy() Creates reference, not a new list Use .copy() or list()
Index out of range Trying to access a non-existent index Check length with len()
Forgetting append() adds to end May need insert() instead Use insert(index, item)
Sorting mixed types E.g. list with str and int Avoid mixing incompatible types

Summary

Operation Syntax
Create list my_list = [1, 2, 3]
Access item my_list[0]
Modify item my_list[1] = 5
Add item my_list.append(4)
Remove item my_list.remove(2)
Loop for item in my_list:
Slice my_list[1:3]

What’s Next?

After learning about lists, explore:

  • Tuples (immutable lists)

  • List comprehensions

  • Dictionaries (key-value pairs)

  • Functions and how to pass lists as arguments