Python Lists

Easy

A Python list is a mutable, ordered collection that can hold items of any type.

Why It Matters

Lists are the most-used data structure in Python. Every coding interview and placement test assumes you know list operations, slicing, and common patterns.

How It Works

Lists are created with [] or list(). They are zero-indexed: first element is lst[0]. Negative indexing: lst[-1] is the last element. Slicing: lst[start:stop:step] returns a new list from index start up to (but not including) stop. Key methods: append() adds to end, extend() merges another iterable, insert(i, x) inserts at position i, remove(x) removes first occurrence, pop(i) removes and returns item at index i. CRITICAL: Assignment creates a reference, not a copy. b = a makes b point to the same list.

Example

lst = [1, 2, 3, 4, 5] lst[1:4] → [2, 3, 4] lst[::-1] → [5, 4, 3, 2, 1] lst.append(6) → [1, 2, 3, 4, 5, 6]

Code Example

# Common gotcha: reference vs copy
a = [1, 2, 3]
b = a          # b is the SAME list
b.append(4)    # a is now [1, 2, 3, 4] too!

# Correct way to copy
c = a[:]       # or a.copy() or list(a)
c.append(5)    # a is unchanged

Common Mistakes

The #1 mistake is confusing append() with extend(). append([4,5]) adds one element (a nested list); extend([4,5]) adds two elements. Another major trap: assigning a list to another variable creates a reference, not a copy.

Ad Space

Test Your Understanding

Easy Q1

What is the output? a = [1, 2, 3] a.append([4, 5]) print(len(a))

Easy Q2

What is the output? a = [1, 2, 3] b = a b.append(4) print(a)

Easy Q3

What does [1, 2, 3, 4, 5][1:4] evaluate to?

Ready to Master Python Lists?

Practice with adaptive questions. Track your misconceptions. Build real mastery.

Sign Up Free & Start Practicing

Free account. No credit card required.

Ad Space

Related Concepts