From 4050b8df4317061744bbdbc39b0654e4a552afda Mon Sep 17 00:00:00 2001 From: Joyce Kung Date: Sat, 18 Jul 2020 18:35:22 -0400 Subject: [PATCH] Move list comp example to lists section from regex --- README.md | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 5d25283..6f6858d 100644 --- a/README.md +++ b/README.md @@ -5212,6 +5212,29 @@ list(zip(nums, letters)) What is List Comprehension? Is it better than a typical loop? Why? Can you demonstrate how to use it?
+
+You have the following list: [{'name': 'Mario', 'food': ['mushrooms', 'goombas']}, {'name': 'Luigi', 'food': ['mushrooms', 'turtles']}] + Extract all type of foods. Final output should be: {'mushrooms', 'goombas', 'turtles'}
+ +``` +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']]) +``` +
+ #### Dictionaries
@@ -5344,29 +5367,6 @@ Using the re module How to find all the IP addresses in a variable? How to find them in a file?
-
-You have the following list: [{'name': 'Mario', 'food': ['mushrooms', 'goombas']}, {'name': 'Luigi', 'food': ['mushrooms', 'turtles']}] - Extract all type of foods. Final output should be: {'mushrooms', 'goombas', 'turtles'}
- -``` -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']]) -``` -
- #### Python Strings