Python Tuples
Python Tuples
Python Tuples
A list in Python is a versatile, mutable, and ordered collection of items. Lists can contain
elements of different data types, such as integers, strings, floats, or even other lists. Unlike
tuples, lists can be changed after creation (they are mutable).
1. Ordered: Items in a list have a defined order, and that order will not change unless
explicitly modified.
2. Mutable: You can change, add, or remove items from a list after it’s been created.
3. Heterogeneous: Lists can contain items of different types (e.g., integers, strings, other
lists).
4. Allows Duplicates: Lists can have repeated elements.
5. Dynamic: Lists can grow or shrink in size as elements are added or removed.
Creating a List:
1. Empty List:
python
Copy code
my_list = []
python
Copy code
my_list = [1, 2, 3, "apple", "banana"]
3. Nested List:
o Lists can contain other lists (nested lists).
python
Copy code
my_list = [1, [2, 3], ["apple", "banana"]]
python
Copy code
my_list = list(range(5)) # Output: [0, 1, 2, 3, 4]
python
Copy code
print(my_list[-1]) # Output: 40
python
Copy code
print(my_list[1:3]) # Output: [20, 30]
Modifying Lists:
python
Copy code
my_list = [10, 20, 30]
my_list[1] = 25
print(my_list) # Output: [10, 25, 30]
2. Adding Elements:
o append(): Adds an element to the end of the list.
python
Copy code
my_list.append(40)
print(my_list) # Output: [10, 25, 30, 40]
python
Copy code
my_list.insert(1, 15)
print(my_list) # Output: [10, 15, 25, 30, 40]
python
Copy code
my_list.extend([50, 60])
print(my_list) # Output: [10, 15, 25, 30, 40, 50, 60]
3. Removing Elements:
o remove(): Removes the first occurrence of a specific element.
python
Copy code
my_list.remove(25)
print(my_list) # Output: [10, 15, 30, 40, 50, 60]
python
Copy code
my_list.pop() # Removes 60
print(my_list) # Output: [10, 15, 30, 40, 50]
python
Copy code
my_list.clear()
print(my_list) # Output: []
List Operations:
python
Copy code
list1 = [1, 2]
list2 = [3, 4]
result = list1 + list2
print(result) # Output: [1, 2, 3, 4]
python
Copy code
my_list = [1, 2]
print(my_list * 3) # Output: [1, 2, 1, 2, 1, 2]
3. Length of a List:
python
Copy code
my_list = [1, 2, 3, 4]
print(len(my_list)) # Output: 4
List Methods:
python
Copy code
my_list = [3, 1, 4, 2]
my_list.sort()
print(my_list) # Output: [1, 2, 3, 4]
2. reverse(): Reverses the elements of the list.
python
Copy code
my_list.reverse()
print(my_list) # Output: [4, 3, 2, 1]
python
Copy code
my_list = [10, 20, 30]
print(my_list.index(20)) # Output: 1
python
Copy code
my_list = [1, 2, 2, 3]
print(my_list.count(2)) # Output: 2
python
Copy code
my_list = [1, 2, 3]
copied_list = my_list.copy()
print(copied_list) # Output: [1, 2, 3]
List Comprehension:
List comprehensions provide a concise way to create lists by applying an expression to each
element in a sequence.
python
Copy code
squares = [x**2 for x in range(5)]
print(squares) # Output: [0, 1, 4, 9, 16]
python
Copy code
even_squares = [x**2 for x in range(10) if x % 2 == 0]
print(even_squares) # Output: [0, 4, 16, 36, 64]
python
Copy code
for index, value in enumerate(my_list):
print(f"Index: {index}, Value: {value}")
List vs Tuple:
# Adding an item
fruits.append("orange")
print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']
# Inserting an item
fruits.insert(1, "blueberry")
print(fruits) # Output: ['apple', 'blueberry', 'banana', 'cherry',
'orange']
# Removing an item
fruits.remove("banana")
print(fruits) # Output: ['apple', 'blueberry', 'cherry', 'orange']
A tuple in Python is an ordered, immutable collection of items. Tuples are similar to lists, but
unlike lists, once a tuple is created, it cannot be modified.
Creating a Tuple:
1. Empty Tuple:
python
Copy code
my_tuple = ()
python
Copy code
my_tuple = (5,)
python
Copy code
my_tuple = (1, 2, 3, "apple", "banana")
python
Copy code
my_tuple = 1, 2, 3, 4
5. Nested Tuple:
o Tuples can contain other tuples.
python
Copy code
my_tuple = (1, (2, 3), (4, 5))
1. Indexing: Access tuple elements using the index (starting from 0).
python
Copy code
my_tuple = ("apple", "banana", "cherry")
print(my_tuple[0]) # Output: apple
python
Copy code
print(my_tuple[-1]) # Output: cherry
python
Copy code
print(my_tuple[1:3]) # Output: ('banana', 'cherry')
Tuple Operations:
python
Copy code
tuple1 = (1, 2, 3)
tuple2 = (4, 5)
result = tuple1 + tuple2
print(result) # Output: (1, 2, 3, 4, 5)
python
Copy code
my_tuple = (1, 2)
print(my_tuple * 3) # Output: (1, 2, 1, 2, 1, 2)
3. Length of a Tuple:
python
Copy code
my_tuple = (1, 2, 3)
print(len(my_tuple)) # Output: 3
Tuple Methods:
python
Copy code
my_tuple = (1, 2, 2, 3, 4)
print(my_tuple.count(2)) # Output: 2
python
Copy code
my_tuple = (1, 2, 3, 4, 2)
print(my_tuple.index(2)) # Output: 1
Tuple Unpacking:
python
Copy code
my_tuple = (1, 2, 3)
a, b, c = my_tuple
print(a, b, c) # Output: 1 2 3
Immutability of Tuples:
python
Copy code
my_tuple = (1, 2, 3)
my_tuple[0] = 5 # Raises TypeError: 'tuple' object does not support
item assignment
python
Copy code
my_tuple.append(4) # Raises AttributeError: 'tuple' object has no
attribute 'append'
Tuple vs List:
Feature Tuple List
Mutability Immutable (cannot be changed) Mutable (can be changed)
Syntax my_tuple = (1, 2, 3) my_list = [1, 2, 3]
Performance Faster due to immutability Slightly slower due to mutability
Use Case Fixed data, read-only operations Dynamic data, frequent changes
Immutable Data: When you need a collection of items that should not be changed
(e.g., coordinates, days of the week).
Memory Efficiency: Tuples use less memory compared to lists, making them suitable
for large datasets that don’t need modification.
Dictionary Keys: Tuples can be used as keys in dictionaries because they are
hashable, unlike lists.