-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComprehensions.py
More file actions
80 lines (70 loc) · 2.77 KB
/
Copy pathComprehensions.py
File metadata and controls
80 lines (70 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# =====================================================================
# FILE: Comprehensions.py
# DESCRIPTION: Comprehensions across lists, dictionaries, sets, and generators.
#
# SYNTAX QUICK-REFERENCE:
# # List Comprehension
# squares = [x**2 for x in range(5) if x % 2 == 0]
#
# # Dictionary Comprehension
# square_dict = {x: x**2 for x in range(3)}
#
# # Set Comprehension
# unique_set = {x for x in [1, 1, 2, 3]}
#
# # Generator Expression (calculates on-the-fly)
# gen_exp = (x**2 for x in range(10000))
# =====================================================================
# Comprehensions.py
# Reference Guide: List, Dictionary, Set, and Generator Comprehensions in Python
# ==========================================
# 1. LIST COMPREHENSIONS
# ==========================================
# Syntax: [expression for item in iterable if condition]
print("--- 1. LIST COMPREHENSIONS ---")
numbers = [1, 2, 3, 4, 5]
# Basic mapping
squares = [x**2 for x in numbers]
print(f"Squares: {squares}")
# Filtering (with if)
evens = [x for x in numbers if x % 2 == 0]
print(f"Evens: {evens}")
# If-Else inside comprehension (expression level)
# Syntax: [expr_if_true if condition else expr_if_false for item in iterable]
labels = ["Even" if x % 2 == 0 else "Odd" for x in numbers]
print(f"Labels: {labels}")
print()
# ==========================================
# 2. DICTIONARY COMPREHENSIONS
# ==========================================
# Syntax: {key_expr: value_expr for item in iterable if condition}
print("--- 2. DICTIONARY COMPREHENSIONS ---")
fruits = ["apple", "banana", "cherry"]
# String length mapping
fruit_lens = {fruit: len(fruit) for fruit in fruits}
print(f"Fruit Lengths: {fruit_lens}")
# Filtering dict comprehension
square_evens = {x: x**2 for x in range(1, 6) if x % 2 == 0}
print(f"Square of evens: {square_evens}")
print()
# ==========================================
# 3. SET COMPREHENSIONS
# ==========================================
# Syntax: {expression for item in iterable if condition}
# Same syntax as dict comprehension but without key-value pairs (colon). Outputs unique elements.
print("--- 3. SET COMPREHENSIONS ---")
duplicates = [1, 2, 2, 3, 4, 4, 5]
unique_squares = {x**2 for x in duplicates}
print(f"Unique Squares Set: {unique_squares}")
print()
# ==========================================
# 4. GENERATOR COMPREHENSIONS (Generator Expressions)
# ==========================================
# Syntax: (expression for item in iterable if condition)
# Returns a generator object instead of a populated collection, saving memory.
print("--- 4. GENERATOR COMPREHENSIONS ---")
gen = (x**2 for x in range(1, 1000000)) # Excludes list allocation in memory
print(f"Generator Type: {type(gen)}")
print(f"First element: {next(gen)}")
print(f"Second element: {next(gen)}")
print()