-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdestructuring.py
More file actions
73 lines (55 loc) · 1.67 KB
/
destructuring.py
File metadata and controls
73 lines (55 loc) · 1.67 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
#https://riptutorial.com/python/example/14981/destructuring-assignment
# destructring or unpacking
# destructing a list
data = [1,2,3]
a, b, c = data
print('last a, b, c :=>', a, b, c)
# destructing a tuple
data = (1,2)
a, b = data
print('tuple a :=>', a, b)
# destructing a set
data = {1,2,2}
a, b = data
print('set a :=>', a, b)
# destructing a frozenset
data = frozenset([1,2,2])
a, b = data
print('frozenset a :=>', a, b)
# destructing a generator
data = (i*i for i in [1,2,3])
a, b, c = data
print('generator a :=>', a, b,c)
# destructing a range object
data = range(1,4)
a, b, c = data
print('range a :=>', a, b,c)
# must unpack all data
data = [1,2,3]
# a, b = data # ValueError: too many values to unpack (expected 2)
# a,b,c,d = data # ValueError: not enough values to unpack (expected 4, got 3)
# unpack values in a variable
data = [1,2,3,4,5]
a, *b = data # b is only referenced
print('a, *b = [1,2,3,4,5] :=> ', a, b)
a, *b, c = data
print('a, *b, c = [1,2,3,4,5] :=> ', a, b, c)
# ignoring values
# use of _ : https://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python
data = [1,2]
a, _ = data
print('a, _ = [1,2] :=> ', a)
data = [1,2,3,4,5]
a, *_ = data
print('a, *_ = [1,2,3,4,5] :=> ', a)
data = [1,2,3,4,5,6,7]
a, _, b, *_ = data
print('a, _, b, *_ = [1,2,3,4,5,6,7] :=> ', a, b)
# destructure a dictionary (but unordered)
# https://www.quora.com/Are-Python-dictionaries-unordered
data = {'name': 'John', 'age': 27, 'email': 'john@example.com'}
a, b, _ = data
print('dict: a, b, _ = data :=> ', a, b)
# destructure dict values (but unordered)
a, b, *_ = (data[key] for key in data)
print('destructure dict values :=> ', a, b)