Python Lists

Python lists are one of the most versatile and frequently used data structures. It is used to store collections of items, such as integers, strings, or even other lists, allowing for flexible manipulation of the items.

Lists written in Python are identical to dynamically scaled arrays defined in other languages, such as Array List in Java and Vector in C++. A list is a collection of items separated by commas and denoted by the symbol [].

What is a List?

  • A list is an ordered collection of items.
  • Lists in Python are mutable, and their content may be modified after creation.
  • Items in a list are separated by commas and placed within square brackets [].

Example:

my_list = [1, 2, 3, 4, 5]

Characteristics of Lists

  1. Heterogeneous Elements: Lists can store items of different data types in the same list.
mixed_list = [1, "apple", 3.14, True]

2. Indexing and Slicing:

  • Every element in a list has an index starting with 0 for the first one.
  • You can access or modify items using their index.
  • Negative indices start from the end of the list and the last item is referred as (-1).
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
print(fruits[-1]) # Output: cherry

Slicing:

  • Extracting a portion of the list.
print(fruits[1:3]) # Output: ['banana', 'cherry']

3. Dynamic Size: Lists can grow or shrink dynamically.

Basic Operations on Lists

1. Creating a List:

numbers = [1, 2, 3, 4]
empty_list = []

2. Adding Elements:

  • append(): Adds a single element to the end of the list.
numbers.append(5)
print(numbers) # Output: [1, 2, 3, 4, 5]
  • extend(): Adds multiple elements to the end.
numbers.extend([6, 7])
print(numbers) # Output: [1, 2, 3, 4, 5, 6, 7]
  • insert(): Inserts an element at a specific position.
numbers.insert(2, 99)
print(numbers) # Output: [1, 2, 99, 3, 4, 5, 6, 7]

3. Removing Elements:

  • remove(): Removes the first occurrence of a specified value.
numbers.remove(99)
print(numbers) # Output: [1, 2, 3, 4, 5, 6, 7]
  • pop(): Removes an element by index and returns it.
last_item = numbers.pop() # Removes the last item
print(last_item) # Output: 7
  • clear(): Removes all elements.
numbers.clear()
print(numbers) # Output: []

4. Modifying Elements:

numbers = [1, 2, 3, 4]
numbers[2] = 99
print(numbers) # Output: [1, 2, 99, 4]

5. Checking Membership:

print(2 in numbers) # Output: True
print(5 in numbers) # Output: False

Useful List Methods

MethodDescription
len()Returns the number of elements in the list.
sort()Sorts the list in ascending order.
reverse()Reverses the order of the list.
index()Returns the index of the first occurrence of a value.
count()Counts how many times a value appears in the list.
copy()Returns a shallow copy of the list.

Advanced Concepts

1. Nested Lists

Lists can contain other lists.

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix[1][2]) # Output: 6

2. List Comprehensions

A concise way to create lists.

squares = [x**2 for x in range(5)]
print(squares) # Output: [0, 1, 4, 9, 16]

Notes:

  • The lists are efficient for lookup and append/remove operations from the end.
  • Modifications at the start or middle end might be slower since it will involve shifting elements.