# 1. Creating Lists
my_list = [1, 2, 3, 4, 5]
empty_list = []
# 2. Accessing elements
first_item = my_list[0] # First element
last_item = my_list[-1] # Last element
sub_list = my_list[1:4] # Slice from index 1 to 3 (4 exclusive)
# 3. Basic List Operations
# Concatenation
concatenated_list = my_list + [6, 7, 8]
# Repetition
repeated_list = [1, 2] * 3 # Output: [1, 2, 1, 2, 1, 2]
# Length of a list
length = len(my_list)
# Membership Test
is_present = 3 in my_list # Returns True if 3 is in my_list
# 4. List Methods
# append()
my_list.append(6) # List becomes [1, 2, 3, 4, 5, 6]
# extend()
my_list.extend([7, 8, 9]) # List becomes [1, 2, 3, 4, 5, 6, 7, 8, 9]
# insert()
my_list.insert(2, 10) # List becomes [1, 2, 10, 3, 4, 5, 6, 7, 8, 9]
# remove()
my_list.remove(10) # List becomes [1, 2, 3, 4, 5, 6, 7, 8, 9]
# pop()
popped_item = my_list.pop() # Removes and returns last element (9)
# index()
index_of_4 = my_list.index(4) # Returns index of first occurrence of 4
# count()
count_of_3 = my_list.count(3) # Returns number of times 3 appears
# sort() and reverse()
my_list.sort() # Sorts the list in ascending order
my_list.reverse() # Reverses the list
# copy()
my_list_copy = my_list.copy() # Copies the list
# clear()
my_list.clear() # Empties the list
# 5. List Comprehensions
# Basic list comprehension
squared_numbers = [x**2 for x in range(5)] # Output: [0, 1, 4, 9, 16]
# List comprehension with condition
even_numbers = [x for x in range(10) if x % 2 == 0] # Output: [0, 2, 4, 6, 8]
# Nested list comprehension
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened = [item for sublist in matrix for item in sublist] # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
# 6. Built-in Functions
# map() – Applies a function to each item
squared = list(map(lambda x: x**2, [1, 2, 3])) # Output: [1, 4, 9]
# filter() – Filters elements based on a condition
even_numbers = list(filter(lambda x: x % 2 == 0, [1, 2, 3, 4])) # Output: [2, 4]
# sorted() – Returns a new sorted list
sorted_list = sorted([5, 3, 9, 1]) # Output: [1, 3, 5, 9]
# min() and max() – Return the smallest and largest element
smallest = min([5, 3, 9, 1]) # Output: 1
largest = max([5, 3, 9, 1]) # Output: 9
# sum() – Sums all elements in the list
total = sum([1, 2, 3, 4]) # Output: 10
# any() – Returns True if any element is True (non-zero, non-empty)
any_true = any([0, 1, 2]) # Output: True
# all() – Returns True if all elements are True
all_true = all([1, 2, 3]) # Output: True
# 7. Advanced List Operations
# Flatten a list of lists
list_of_lists = [[1, 2], [3, 4], [5, 6]]
flattened = [item for sublist in list_of_lists for item in sublist] # Output: [1, 2, 3, 4, 5, 6]
# Find intersection of two lists
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
intersection = list(set(list1) & set(list2)) # Output: [3, 4]
# Find union of two lists
union = list(set(list1) | set(list2)) # Output: [1, 2, 3, 4, 5, 6]
# 8. More Complex Operations
# Zipping two lists
list_a = [1, 2, 3]
list_b = ['a', 'b', 'c']
zipped = list(zip(list_a, list_b)) # Output: [(1, 'a'), (2, 'b'), (3, 'c')]
# Unzipping a list of tuples
unzipped_a, unzipped_b = zip(*zipped) # Output: (1, 2, 3), ('a', 'b', 'c')