A list in Python is an ordered, mutable (changeable), and iterable collection used to store multiple items in a single variable. Lists can contain elements of different data types (integers, strings, floats, even other lists).
* Ordered – Items are stored in a specific order and can be accessed using an index.
* Mutable – You can modify a list by adding, removing, or changing elements.
* Allows Duplicates – Lists can contain duplicate values.
* Heterogeneous – A list can store different data types (integers, strings, lists, etc.).
* Dynamic Size – Lists can grow or shrink dynamically as elements are added or removed.
my_list = []
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed_list = [1, "hello", 3.14, True]
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
print(fruits[-1]) # Output: cherry (negative index counts from the end)
Using Slicing :
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4]) # Output: [20, 30, 40]
print(numbers[:3]) # Output: [10, 20, 30]
print(numbers[-3:]) # Output: [30, 40, 50]
fruits.append("orange") # Adds at the end
fruits.insert(1, "grape") # Inserts at index 1
print(fruits) # Output: ['apple', 'grape', 'banana', 'cherry', 'orange']?
fruits.remove("banana") # Removes "banana"
popped_item = fruits.pop(2) # Removes element at index 2
del fruits[0] # Deletes first element
fruits.clear() # Empties the list?
fruits[1] = "blueberry" # Change "banana" to "blueberry"?
for fruit in fruits:
print(fruit)?
squared_numbers = [x**2 for x in [1, 2, 3, 4, 5]]
print(squared_numbers) # Output: [1, 4, 9, 16, 25]?
Method | Description | Example |
---|---|---|
append(x) |
Adds an element to the end | list.append(10) |
insert(i, x) |
Inserts an element at index i |
list.insert(1, 20) |
remove(x) |
Removes first occurrence of x |
list.remove(5) |
pop(i) |
Removes element at index i |
list.pop(2) |
clear() |
Removes all elements | list.clear() |
sort() |
Sorts the list (ascending by default) | list.sort() |
reverse() |
Reverses the list order | list.reverse() |
count(x) |
Counts occurrences of x |
list.count(2) |
index(x) |
Returns index of first occurrence of x |
list.index(3) |
students = ["Alice", "Bob", "Charlie"]
students.append("David")
students.remove("Bob")
print(students) # Output: ['Alice', 'Charlie', 'David']?
A tuple in Python is an ordered, immutable (unchangeable), and iterable collection used to store multiple items in a single variable. Unlike lists, tuples cannot be modified after creation, making them useful for storing fixed data.
* Ordered – Items maintain a specific sequence and can be accessed via indexing.
* Immutable – Once created, elements cannot be changed, added, or removed.
* Allows Duplicates – Can store repeated values.
* Heterogeneous – Can store multiple data types (integers, strings, lists, etc.).
* Memory Efficient & Faster – Uses less memory and is faster than lists due to immutability.
* Hashable – Tuples can be used as dictionary keys if they contain only immutable elements.
empty_tuple = ()
empty_tuple = tuple() # Another way?
fruits = ("apple", "banana", "cherry")
numbers = (1, 2, 3, 4, 5)
mixed_tuple = (1, "hello", 3.14, True)?
single_element_tuple = ("hello",) # ? Correct
not_a_tuple = ("hello") # ? This is just a string?
fruits = ("apple", "banana", "cherry")
print(fruits[0]) # Output: apple
print(fruits[-1]) # Output: cherry (negative index counts from the end)?
numbers = (10, 20, 30, 40, 50)
print(numbers[1:4]) # Output: (20, 30, 40)
print(numbers[:3]) # Output: (10, 20, 30)
print(numbers[-3:]) # Output: (30, 40, 50)?
fruits = ("apple", "banana", "cherry")
fruits[1] = "blueberry" # ? TypeError: 'tuple' object does not support item assignment?
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
merged_tuple = tuple1 + tuple2
print(merged_tuple) # Output: (1, 2, 3, 4, 5, 6)?
numbers = (1, 2, 3)
repeated = numbers * 3
print(repeated) # Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)?
person = ("John", 30, "Engineer")
name, age, profession = person
print(name) # Output: John
print(age) # Output: 30
print(profession) # Output: Engineer?
Method | Description | Example |
---|---|---|
count(x) |
Counts occurrences of x |
tuple.count(2) |
index(x) |
Returns index of first occurrence of x |
tuple.index(3) |
* When data should not be modified (e.g., days of the week, fixed coordinates).
* When you need faster access and iteration (tuples are faster than lists).
* When using dictionary keys (tuples are hashable if they contain only immutable elements).
Feature | List | Tuple |
---|---|---|
Mutability | Mutable (can be modified) ? | Immutable (cannot be modified after creation) ? |
Syntax | Defined using square brackets [ ] |
Defined using parentheses ( ) |
Performance | Slower due to dynamic resizing and modification overhead ? | Faster due to immutability and fixed structure ? |
Memory Usage | Takes more memory ? | Takes less memory due to immutability ? |
Operations | Supports append(), remove(), pop(), sort(), etc. | Limited operations (count(), index()) |
Use Case | Suitable for dynamic data where modification is needed (e.g., lists of users, tasks, etc.) | Suitable for fixed data that should not change (e.g., coordinates, database records, constants) |
Iteration Speed | Slower because of modifiable structure | Faster due to fixed size |
Hashability | Not hashable (cannot be used as dictionary keys) ? | Hashable if all elements inside are immutable ? |
my_list = [1, 2, 3]
my_list.append(4) # ? Allowed
my_list[0] = 100 # ? Allowed
print(my_list) # Output: [100, 2, 3, 4]?
my_tuple = (1, 2, 3)
my_tuple[0] = 100 # ? Error: TypeError: 'tuple' object does not support item assignment?