Skip to content

Commit 84fa580

Browse files
author
jairoufps
committed
added snnipet that show usea list comprehension
1 parent 9778644 commit 84fa580

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

snippets/python/list_comprehension.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Show use of list comprehension
2+
*tags:* **list, list comprehension**
3+
4+
5+
**Snippet**
6+
```python
7+
8+
9+
num_list = [2,3,6,7,8,9,0]
10+
11+
def pow_base_two(num):
12+
return pow(num,2)
13+
14+
#Solution with list comprehension
15+
16+
num_result_pow = [pow_base_two(num) for num in num_list ]
17+
18+
print(num_result_pow)
19+
20+
21+
```
22+
23+
24+
```python
25+
26+
27+
num_list = [2,3,6,7,8,9,0]
28+
29+
def pow_base_two(num):
30+
return pow(num,2)
31+
32+
#Solution without list comprehension
33+
34+
num_result_pow = []
35+
36+
for num in num_list:
37+
num_result_pow.append(pow_base_two(num))
38+
39+
print(num_result_pow)
40+
41+
```

0 commit comments

Comments
 (0)