- Python Features
- Python Installation
- PyCharm Configuration
- Python Variables
- Python Data Types
- Python If Else
- Python Loops
- Python Strings
- Python Lists
- Python Tuples
- Python List Vs Tuple
- Python Sets
- Python Dictionary
- Python Functions
- Python Files I/O
- Read Write Excel
- Read Write JSON
- Read Write CSV
- Python OS Module
- Python Exceptions
- Python Datetime
- Python Collection Module
- Python Sys Module
- Python Decorator
- Python Generators
- Python OOPS
- Python Numpy Module
- Python Pandas Module
- Python Sqlite Module
List Comprehension
List comprehension is a concise and expressive way of creating lists in Python. It allows you to define a list and its elements within a single line of code, combining the functionality of a for loop, optional if conditions, and even nested loops. List comprehensions are preferred for their readability and efficiency compared to traditional for loops.
- Simple For Loop List Comprehension:
This type of list comprehension is used to create a list by iterating over elements from an iterable (e.g., list, tuple, string) without any filtering or conditions. Here’s an example:
# Example list
numbers = [1, 2, 3, 4, 5]
# List comprehension without filtering or conditions
new_list = [num for num in numbers]
print(new_list) # Output: [1, 2, 3, 4, 5]
- Loop and If Condition List Comprehension:
This type of list comprehension includes an if condition to filter elements while iterating over the iterable. Only elements that satisfy the condition are included in the new list. Here’s an example:
# Example list
numbers = [1, 2, 3, 4, 5, 6]
# List comprehension with an if condition
even_numbers = [num for num in numbers if num % 2 == 0]
print(even_numbers) # Output: [2, 4, 6]
- Loop and If-Else Condition List Comprehension:
# Example list
numbers = [1, 2, 3, 4, 5, 6]
# List comprehension with if-else condition
result = ['Even' if num % 2 == 0 else 'Odd' for num in numbers]
print(result)
# Output: ['Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even']
This type of list comprehension allows you to perform different operations based on the if-else condition while iterating over the iterable. Here’s an example:
- Nested Loop List Comprehension:
This type of list comprehension allows you to use nested loops for creating more complex lists by iterating over multiple iterables simultaneously. Here’s an example:
# Example nested loops
colors = ['red', 'green', 'blue']
objects = ['ball', 'box', 'pen']
# List comprehension with nested loops
combinations = [f"{color} {obj}" for color in colors for obj in objects]
print(combinations)
# Output: ['red ball', 'red box', 'red pen', 'green ball', 'green box', 'green pen', 'blue ball', 'blue box', 'blue pen']
Shallow Copy and Deep Copy of Lists in Python:
In Python, when dealing with lists or other compound data structures, understanding the concepts of shallow copy and deep copy is crucial. Both concepts involve creating a new copy of an existing list, but they differ in how they handle nested objects within the list.
- Shallow Copy:
A shallow copy of a list creates a new list object but does not create copies of the elements inside the list. Instead, it copies references to the original objects. This means that changes made to nested objects within the copied list will affect the original list, and vice versa. To perform a shallow copy, you can use the `copy()` method or the slicing notation `[:]`. Example of Shallow Copy:
import copy
# Original list with a nested list
original_list = [[1, 2, 3], [4, 5, 6]]
# Shallow copy using the copy() method
shallow_copy1 = original_list.copy()
# Shallow copy using slicing notation
shallow_copy2 = original_list[:]
# Modifying a nested element in the copied list
shallow_copy1[0][0] = 99
print("Original List:", original_list)
# Output: Original List: [[99, 2, 3], [4, 5, 6]]
print("Shallow Copy 1:", shallow_copy1)
# Output: Shallow Copy 1: [[99, 2, 3], [4, 5, 6]]
print("Shallow Copy 2:", shallow_copy2)
# Output: Shallow Copy 2: [[99, 2, 3], [4, 5, 6]]
- Deep Copy:
A deep copy of a list creates a new list object and also recursively creates copies of all the nested objects within the original list. In other words, the copied list and its nested objects are entirely independent of the original list and its nested objects. To perform a deep copy, you need to use the `deepcopy()` function from the `copy` module. Example of Deep Copy:
import copy
# Original list with nested lists
original_list = [[1, 2, 3], [4, 5, 6]]
# Creating a deep copy
deep_copy = copy.deepcopy(original_list)
# Modifying a nested element in the deep copy
deep_copy[0][0] = 99
print("Original List:", original_list)
# Output: Original List: [[1, 2, 3], [4, 5, 6]]
print("Deep Copy:", deep_copy)
# Output: Deep Copy: [[99, 2, 3], [4, 5, 6]]
Shallow copy is faster and suitable when you want to create a new list but share references to nested objects. Deep copy is appropriate when you need an entirely independent copy of the original list and all its nested objects.