PYTHON • Beginner

Python Lists

A list is an ordered, mutable collection of items. Lists are one of the most-used data structures in Python — you'll use them in almost every program. They can hold mixed data types and can be modified after creation.

Creating a List

# Empty list
empty = []

# List of numbers
numbers = [1, 2, 3, 4, 5]

# List of strings
fruits = ["apple", "banana", "mango"]

# Mixed types
mixed = [1, "hello", 3.14, True]

# Using list() constructor
chars = list("Python")   # ['P', 'y', 't', 'h', 'o', 'n']

Accessing Items

Use index (starts at 0) or negative index (from end).

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

print(fruits[0])    # apple
print(fruits[1])    # banana
print(fruits[-1])   # mango
print(fruits[-2])   # banana

Slicing Lists

nums = [10, 20, 30, 40, 50]

print(nums[1:4])    # [20, 30, 40]
print(nums[:3])     # [10, 20, 30]
print(nums[2:])     # [30, 40, 50]
print(nums[::-1])   # [50, 40, 30, 20, 10] (reversed)

Modifying Lists

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

# Change an item
fruits[1] = "orange"
print(fruits)   # ['apple', 'orange', 'mango']

# Add at end
fruits.append("grape")

# Add at position
fruits.insert(0, "kiwi")

# Add multiple
fruits.extend(["pear", "peach"])

print(fruits)

Removing Items

nums = [10, 20, 30, 40, 20]

nums.remove(20)         # Removes first occurrence of 20
nums.pop()              # Removes last item
nums.pop(0)             # Removes index 0
del nums[0]             # Deletes index 0
nums.clear()            # Removes everything

print(nums)

Useful List Methods

nums = [30, 10, 20, 50, 40]

print(len(nums))            # 5 (length)
print(max(nums))            # 50
print(min(nums))            # 10
print(sum(nums))            # 150
print(20 in nums)           # True (membership)

nums.sort()                 # Sorts in-place
print(nums)                 # [10, 20, 30, 40, 50]

nums.sort(reverse=True)     # Descending
nums.reverse()              # Reverses order
print(nums.count(20))       # Counts occurrences
print(nums.index(30))       # Index of first 30

Looping Through a List

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

# Basic loop
for fruit in fruits:
    print(fruit)

# With index
for i, fruit in enumerate(fruits):
    print(i, fruit)

List Comprehension

A concise way to create lists in a single line. This is a very Pythonic feature and often asked in interviews.

# Squares of 1 to 5
squares = [x**2 for x in range(1, 6)]
print(squares)   # [1, 4, 9, 16, 25]

# Even numbers from 1 to 10
evens = [x for x in range(1, 11) if x % 2 == 0]
print(evens)     # [2, 4, 6, 8, 10]

# Uppercase all fruits
fruits = ["apple", "banana"]
upper = [f.upper() for f in fruits]
print(upper)     # ['APPLE', 'BANANA']
💡 Academy Tip

List comprehensions are faster than writing a for loop that appends to an empty list. Use them for short, readable transformations — not for complex logic.

Nested Lists (2D Lists)

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

print(matrix[0])       # [1, 2, 3]
print(matrix[1][2])    # 6 (second row, third column)

# Loop through 2D list
for row in matrix:
    for num in row:
        print(num, end=" ")
    print()
⚠ Common Mistake

Do not copy a list by simple assignment: b = a — both variables point to the same list. To make a copy, use b = a.copy() or b = a[:].

📝 Try Yourself

Create a list of 5 numbers. Print: the sum, the average, the largest, the smallest, and a sorted copy of the list. Then use list comprehension to create a new list with only numbers greater than the average.

Quick Revision

  • Lists are ordered, mutable, allow duplicates.
  • Created with [] or list().
  • Indexing starts at 0; negative from end.
  • Methods: append, insert, remove, pop, sort, reverse, extend, clear.
  • Built-in: len, max, min, sum.
  • List comprehension is a fast, Pythonic way to build lists.
  • Copy with .copy() or [:] — not with =.

Interview Questions

Q: Difference between append() and extend()?

A: append() adds a single item (as-is) to the end. extend() adds each item of an iterable individually. Example: [1,2].append([3,4]) → [1,2,[3,4]]. [1,2].extend([3,4]) → [1,2,3,4].

Q: What is list comprehension? Give an example.

A: It's a compact way to create lists using a single line. Example: [x*2 for x in range(5)] → [0, 2, 4, 6, 8]. It's faster and more readable than a loop.

Q: How do you remove duplicates from a list?

A: Convert to a set: list(set(my_list)). This removes duplicates but loses order. To preserve order, use dict.fromkeys(my_list).

Q: Difference between remove() and pop()?

A: remove(x) removes the first occurrence of value x. pop(i) removes and returns the item at index i. pop() without argument removes the last item.

Want to Master Python?

Join our Python course in Varanasi — practical classes and career support at our Sarnath centre.

Ask on WhatsApp
Chat with us