mirror of
https://github.com/bregman-arie/devops-exercises.git
synced 2024-11-24 08:22:12 +02:00
Move list comp example to lists section from regex
This commit is contained in:
parent
9c327e5a6b
commit
4050b8df43
46
README.md
46
README.md
@ -5212,6 +5212,29 @@ list(zip(nums, letters))
|
||||
<summary>What is List Comprehension? Is it better than a typical loop? Why? Can you demonstrate how to use it?</summary><br><b>
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>You have the following list: <code>[{'name': 'Mario', 'food': ['mushrooms', 'goombas']}, {'name': 'Luigi', 'food': ['mushrooms', 'turtles']}]</code>
|
||||
Extract all type of foods. Final output should be: {'mushrooms', 'goombas', 'turtles'}</summary><br><b>
|
||||
|
||||
```
|
||||
brothers_menu = \
|
||||
[{'name': 'Mario', 'food': ['mushrooms', 'goombas']}, {'name': 'Luigi', 'food': ['mushrooms', 'turtles']}]
|
||||
|
||||
# "Classic" Way
|
||||
def get_food(brothers_menu) -> set:
|
||||
temp = []
|
||||
|
||||
for brother in brothers_menu:
|
||||
for food in brother['food']:
|
||||
temp.append(food)
|
||||
|
||||
return set(temp)
|
||||
|
||||
# One liner way (Using list comprehension)
|
||||
set([food for bro in x for food in bro['food']])
|
||||
```
|
||||
</b></details>
|
||||
|
||||
#### Dictionaries
|
||||
|
||||
<details>
|
||||
@ -5344,29 +5367,6 @@ Using the re module
|
||||
<summary>How to find all the IP addresses in a variable? How to find them in a file?</summary><br><b>
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>You have the following list: <code>[{'name': 'Mario', 'food': ['mushrooms', 'goombas']}, {'name': 'Luigi', 'food': ['mushrooms', 'turtles']}]</code>
|
||||
Extract all type of foods. Final output should be: {'mushrooms', 'goombas', 'turtles'}</summary><br><b>
|
||||
|
||||
```
|
||||
brothers_menu = \
|
||||
[{'name': 'Mario', 'food': ['mushrooms', 'goombas']}, {'name': 'Luigi', 'food': ['mushrooms', 'turtles']}]
|
||||
|
||||
# "Classic" Way
|
||||
def get_food(brothers_menu) -> set:
|
||||
temp = []
|
||||
|
||||
for brother in brothers_menu:
|
||||
for food in brother['food']:
|
||||
temp.append(food)
|
||||
|
||||
return set(temp)
|
||||
|
||||
# One liner way (Using list comprehension)
|
||||
set([food for bro in x for food in bro['food']])
|
||||
```
|
||||
</b></details>
|
||||
|
||||
#### Python Strings
|
||||
|
||||
<details>
|
||||
|
Loading…
Reference in New Issue
Block a user