Let’s explore some more advanced list operations, including list slicing, list comprehensions with conditions, merging lists, flattening nested lists, and using itertools
for combinations and permutations.
import itertools
# Creating a list of integers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# 1. List Slicing: Get every second element
every_second = numbers[::2]
# 2. Conditional List Comprehension: Create a list of numbers greater than 5
greater_than_five = [x for x in numbers if x > 5]
# 3. Merging two lists element-wise using zip()
list_a = [1, 2, 3]
list_b = ['a', 'b', 'c']
merged_list = [(x, y) for x, y in zip(list_a, list_b)]
# 4. Flattening a nested list
nested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
flattened_list = [item for sublist in nested_list for item in sublist]
# 5. Using itertools to generate combinations and permutations
combinations = list(itertools.combinations(numbers, 2))
permutations = list(itertools.permutations(numbers[:3]))
# Print the results
print("Original numbers:", numbers)
print("Every second element:", every_second)
print("Numbers greater than five:", greater_than_five)
print("Merged list:", merged_list)
print("Flattened list:", flattened_list)
print("Combinations of 2 elements:", combinations)
print("Permutations of first 3 elements:", permutations)
every_second = numbers[::2]
This creates a new list every_second
that contains every second element from the original list numbers
. The slicing syntax [::2]
means “start to end with a step of 2”.
greater_than_five = [x for x in numbers if x > 5]
This list comprehension filters the elements of numbers
to include only those greater than 5.
merged_list = [(x, y) for x, y in zip(list_a, list_b)]
The zip()
function pairs elements from list_a
and list_b
, creating tuples of corresponding elements. The list comprehension then constructs a list of these tuples.
flattened_list = [item for sublist in nested_list for item in sublist]
This double list comprehension flattens nested_list
by iterating over each sublist and then over each item in those sublists, collecting all items into a single list.
combinations = list(itertools.combinations(numbers, 2))
permutations = list(itertools.permutations(numbers[:3]))
itertools.combinations(numbers, 2)
generates all possible combinations of 2 elements from numbers
.itertools.permutations(numbers[:3])
generates all possible permutations of the first 3 elements of numbers
.Original numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Every second element: [1, 3, 5, 7, 9]
Numbers greater than five: [6, 7, 8, 9, 10]
Merged list: [(1, 'a'), (2, 'b'), (3, 'c')]
Flattened list: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Combinations of 2 elements: [(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (2, 10), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (3, 10), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (4, 10), (5, 6), (5, 7), (5, 8), (5, 9), (5, 10), (6, 7), (6, 8), (6, 9), (6, 10), (7, 8), (7, 9), (7, 10), (8, 9), (8, 10), (9, 10)]
Permutations of first 3 elements: [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]