Lists in Python
A list is an ordered collection of elements, which can be of any data type โ integers, strings, floats, or even other lists. Lists are created using [ ] (square brackets).Example:
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = [10, "Python", 3.14, True]
You can access elements in a list using indexing (starting from 0).
print(fruits[0]) # Output: apple
print(fruits[-1]) # Output: cherry
List Operations
Python provides several operations to manipulate lists.| Operation | Description | Example |
|---|---|---|
| append() | Adds an item to the end | fruits.append("mango") |
| insert() | Inserts item at a specific index | fruits.insert(1, "orange") |
| extend() | Adds multiple items from another list | fruits.extend(["grape", "melon"]) |
| remove() | Removes the first occurrence of a value | fruits.remove("banana") |
| pop() | Removes item by index (default last) | fruits.pop() |
| sort() | Sorts list in ascending order | numbers.sort() |
| reverse() | Reverses the list | numbers.reverse() |
| clear() | Removes all items | fruits.clear() |
# Initial list
fruits = ["apple", "banana", "cherry"]
print(fruits) # ['apple', 'banana', 'cherry']
numbers = [5, 2, 9, 1]
print(numbers) # [5, 2, 9, 1]
# 1. append()
fruits.append("mango")
print(fruits) # ['apple', 'banana', 'cherry', 'mango']
# 2. insert()
fruits.insert(1, "orange")
print(fruits) # ['apple', 'orange', 'banana', 'cherry', 'mango']
# 3. extend()
fruits.extend(["grape", "melon"])
print(fruits) # ['apple', 'orange', 'banana', 'cherry', 'mango', 'grape', 'melon']
# 4. remove()
fruits.remove("banana")
print(fruits) # ['apple', 'orange', 'cherry', 'mango', 'grape', 'melon']
# 5. pop()
popped_item = fruits.pop()
print(fruits, "|", popped_item) # ['apple', 'orange', 'cherry', 'mango', 'grape'] | melon
# 6. sort()
numbers.sort()
print(numbers) # [1, 2, 5, 9]
# 7. reverse()
numbers.reverse()
print(numbers) # [9, 5, 2, 1]
# 8. clear()
fruits.clear()
print(fruits) # []
Indexing and Slicing
Lists support indexing and slicing similar to strings.numbers = [10, 20, 30, 40, 50]
print(numbers[1:4]) # Output: [20, 30, 40]
print(numbers[:3]) # Output: [10, 20, 30]
print(numbers[-2:]) # Output: [40, 50]
You can also use nested lists:
data = [[1, 2], [3, 4], [5, 6]]
print(data[1][0]) # Output: 3
Tuples in Python
A tuple is similar to a list but immutable. Once created, its elements cannot be modified. Tuples are defined using ( ) (parentheses).Example:
colors = ("red", "green", "blue")
print(colors[0]) # Output: red
Attempting to modify a tuple will cause an error:
colors[1] = "yellow" # TypeError: 'tuple' object does not support item assignment
The immutability of tuples makes them faster and safer when working with fixed data that should not change. This feature is often used in constants, dictionary keys, and function returns.
Tuple Unpacking
Tuple unpacking allows you to assign tuple elements to multiple variables in one step.Example:
person = ("Alice", 25, "Engineer")
name, age, profession = person
print(name) # Output: Alice
print(age) # Output: 25
print(profession) # Output: Engineer
You can also use the * operator for variable-length unpacking:
numbers = (1, 2, 3, 4, 5)
a, *b, c = numbers
print(a) # 1
print(b) # [2, 3, 4]
print(c) # 5
Summary
Lists and Tuples are core building blocks in Python programming. While lists offer flexibility and mutability for dynamic data, tuples ensure stability and efficiency for fixed data.| Concept | Description |
|---|---|
| List | Mutable ordered collection of elements |
| Tuple | Immutable ordered collection of elements |
| Indexing | Access elements using positions (starting from 0) |
| Slicing | Extract a portion of a list or tuple |
| List Operations | Methods like append(), insert(), pop(), sort() |
| Immutability | Tuples cannot be modified after creation |
| Unpacking | Assign multiple variables from a tuple or list easily |