Working with Lists in Python: Indexing, Methods, and Iteration

Defining a List

A list in Python is a collection defined by square brackets [] with items separated by commas. Lists are versatile; they can hold mixed data types, including integers, strings, and even other lists (nested structures).

# Initializing empty lists
items = []
items = list()

# Creating a nested list
matrix = [[1, 2, 3], [4, 5, 6]]
print(matrix)
# Output: [[1, 2, 3], [4, 5, 6]]

Accessing Elements via Index

Lists are ordered sequences, meaning every item has a specific position. Indexing starts at 0 for the first element. You can also use negative indexing, where -1 refers to the last item.

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

# Positive indexing
print(fruits[0])  # apple
print(fruits[1])  # banana

# Negative indexing
print(fruits[-1]) # cherry
print(fruits[-2]) # banana

# Accessing nested list elements
grid = [[10, 20], [30, 40]]
print(grid[1][0]) # 30
print(grid[0][1]) # 20

Note: Attempting to access an index outside the list's range will raise an IndexError.

Common List Operations

Python lists come with a variety of built-in methods for manipulation:

  • index(item): Find the index of the first occurrence of an item.
  • insert(i, x): Insert an item at a given position.
  • append(x): Add an item to the end of the list.
  • extend(iterable): Add items from an iterable to the end.
  • pop([i]): Remove and return the item at the given position (default is the last).
  • remove(x): Remove the first occurrence of a value.
  • clear(): Remove all items from the list.
  • count(x): Return the number of times a value appears.
  • len(list): Return the number of items in the list.
data = ['a', 'b', 'c', 'b']

# Find index
print(data.index('b'))  # 1

# Insert
data.insert(1, 'x')
print(data)  # ['a', 'x', 'b', 'c', 'b']

# Append
data.append('z')
print(data)  # ['a', 'x', 'b', 'c', 'b', 'z']

# Extend
data.extend([1, 2])
print(data)  # ['a', 'x', 'b', 'c', 'b', 'z', 1, 2]

# Pop
removed = data.pop(1)
print(removed)  # x
print(data)     # ['a', 'b', 'c', 'b', 'z', 1, 2]

# Remove
data.remove('b')
print(data)  # ['a', 'c', 'b', 'z', 1, 2]

# Count and Length
nums = [1, 2, 2, 3, 2]
print(nums.count(2))  # 3
print(len(nums))      # 5

# Clear
nums.clear()
print(nums)  # []

# Concatenation and Repetition
list_a = [1, 2]
list_b = [3, 4]
print(list_a + list_b)  # [1, 2, 3, 4]
print(list_a * 3)       # [1, 2, 1, 2, 1, 2]

Characteristics of Lists

  1. They can store multiple items.
  2. They support heterogeneous data types (mixed types).
  3. They are ordered (items retain their position).
  4. They allow duplicate values.
  5. They are mutable (can be changed after creation).

Iterating Through a List

Iteration is the process of going through each item in a collection.

Using While Loop

A while loop requires manual management of the index counter.

colors = ['red', 'green', 'blue']
cursor = 0

while cursor < len(colors):
    print(colors[cursor])
    cursor += 1

Using For Loop

The for loop is more concise for iterating over sequences as it handles the index automatically.

colors = ['red', 'green', 'blue']

for shade in colors:
    print(shade)

Comparison: While vs For

  • Control: while loops use custom conditions; for loops iterate strictly over items.
  • Infinite Loops: while loops can easily run infinitely; for loops are bound by the size of the collection.
  • Use Case: Use while for general looping logic; use for specifically for traversing data containers.

Tags: python Python Lists Data Structures Iteration indexing

Posted on Sat, 09 May 2026 14:52:02 +0000 by sunil_23413