Добавить
Уведомления

Dictionary Comprehension: 7 Ways To Use In Python (2 Min)

In this tutorial, you'll learn how to use the dictionary comprehension syntax in Python. — Facebook: https://www.facebook.com/GokceDBsql — Video Transcript: — Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn seven ways to use the dictionary comprehension syntax in Python. Number one the syntax for dictionary comprehension is the key colon value for the item in iterable with an optional condition in curly braces. Here, I'm creating a new dictionary called new dict 1 from an existing dictionary if the value contains the keyword property. However, you can do the same things in one line using the dictionary comprehension syntax. Number two, you can also change the name of the keys in the new dictionary. Here I'm duplicating the name twice when creating the new dictionary called new underscore dict 2. Number three the if condition in the dictionary comprehension syntax is optional. In this example, I got rid of the if the property was in value condition. Number four, you can also use the range function as the iterable to say store the squares of numbers. Here I'm storing the squares of numbers in the range of 1 to 10. Number 5 you can have multiple if conditions in the dictionary comprehension syntax. Here I'm saying only store the squares of numbers if the number is even and the number is greater than five and the number is less than eight. Number six, you can also use if else in the key colon value section. Here I'm saying to store the value in the key-value pair as odd if n percent 2 is not equal to 0 else make it even. Number seven you can also use a nested dictionary comprehension syntax if you are parsing a nested dictionary. In this example, I'm creating a new nested dictionary called new underscore dict 7. If the outer key contains the keyword property. There you have it. Make sure you like, subscribe, and turn on the notification bell. Until next time. Connect MySQL In Python: https://www.youtube.com/watch?v=0qBctY82et0 Run Selenium PyTests In Parallel: https://www.youtube.com/watch?v=cQE6ABZ9I14 Find_elements() in Selenium: https://www.youtube.com/watch?v=uqWc_WyfTCI — # 1. Dictionary Comprehension Syntax {key:value for item in iterable (if condition)} dict1 = {'table1': 'property_info', 'table2': 'property_sales', 'table3': 'customer_info'} new_dict1 = {} for k, v in dict1.items(): if 'property' in v: new_dict1[k] = v print("1a:", new_dict1) new_dict1.clear() new_dict1 = {k: v for k, v in dict1.items() if 'property' in v} print("1b:", new_dict1) # 2. Change name of the keys in the new dictionary new_dict2 = {k*2: v for k, v in dict1.items() if 'property' in v} print("2:", new_dict2) # 3. If condition is optional new_dict3 = {k*2: v for k, v in dict1.items()} print("3:", new_dict3) # 4. Use with range function to say store squares of numbers new_dict4 = {n: n**2 for n in range(1, 10)} print("4:", new_dict4) # 5. Multiple if conditions new_dict5 = {n: n**2 for n in range(1, 10) if n % 2 == 0 if n [] 5 if n [] 8} print("5:", new_dict5) # 6. if/else in key:value new_dict6 = {n: ('odd' if n % 2 != 0 else 'even') for n in range(1, 10)} print("6:", new_dict6) # 7. Nested dictionary comprehension dict7 = {'property_info': {'rows': 10}, 'property_sales': {'rows': 20}, 'customer_info': {'rows': 30}} new_dict7 = {o_k: {i_k: i_v for i_k, i_v in o_v.items()} for o_k, o_v in dict7.items() if 'property' in o_k} print("7:", new_dict7) --

Иконка канала Python обучение
11 подписчиков
12+
16 просмотров
2 года назад
12+
16 просмотров
2 года назад

In this tutorial, you'll learn how to use the dictionary comprehension syntax in Python. — Facebook: https://www.facebook.com/GokceDBsql — Video Transcript: — Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn seven ways to use the dictionary comprehension syntax in Python. Number one the syntax for dictionary comprehension is the key colon value for the item in iterable with an optional condition in curly braces. Here, I'm creating a new dictionary called new dict 1 from an existing dictionary if the value contains the keyword property. However, you can do the same things in one line using the dictionary comprehension syntax. Number two, you can also change the name of the keys in the new dictionary. Here I'm duplicating the name twice when creating the new dictionary called new underscore dict 2. Number three the if condition in the dictionary comprehension syntax is optional. In this example, I got rid of the if the property was in value condition. Number four, you can also use the range function as the iterable to say store the squares of numbers. Here I'm storing the squares of numbers in the range of 1 to 10. Number 5 you can have multiple if conditions in the dictionary comprehension syntax. Here I'm saying only store the squares of numbers if the number is even and the number is greater than five and the number is less than eight. Number six, you can also use if else in the key colon value section. Here I'm saying to store the value in the key-value pair as odd if n percent 2 is not equal to 0 else make it even. Number seven you can also use a nested dictionary comprehension syntax if you are parsing a nested dictionary. In this example, I'm creating a new nested dictionary called new underscore dict 7. If the outer key contains the keyword property. There you have it. Make sure you like, subscribe, and turn on the notification bell. Until next time. Connect MySQL In Python: https://www.youtube.com/watch?v=0qBctY82et0 Run Selenium PyTests In Parallel: https://www.youtube.com/watch?v=cQE6ABZ9I14 Find_elements() in Selenium: https://www.youtube.com/watch?v=uqWc_WyfTCI — # 1. Dictionary Comprehension Syntax {key:value for item in iterable (if condition)} dict1 = {'table1': 'property_info', 'table2': 'property_sales', 'table3': 'customer_info'} new_dict1 = {} for k, v in dict1.items(): if 'property' in v: new_dict1[k] = v print("1a:", new_dict1) new_dict1.clear() new_dict1 = {k: v for k, v in dict1.items() if 'property' in v} print("1b:", new_dict1) # 2. Change name of the keys in the new dictionary new_dict2 = {k*2: v for k, v in dict1.items() if 'property' in v} print("2:", new_dict2) # 3. If condition is optional new_dict3 = {k*2: v for k, v in dict1.items()} print("3:", new_dict3) # 4. Use with range function to say store squares of numbers new_dict4 = {n: n**2 for n in range(1, 10)} print("4:", new_dict4) # 5. Multiple if conditions new_dict5 = {n: n**2 for n in range(1, 10) if n % 2 == 0 if n [] 5 if n [] 8} print("5:", new_dict5) # 6. if/else in key:value new_dict6 = {n: ('odd' if n % 2 != 0 else 'even') for n in range(1, 10)} print("6:", new_dict6) # 7. Nested dictionary comprehension dict7 = {'property_info': {'rows': 10}, 'property_sales': {'rows': 20}, 'customer_info': {'rows': 30}} new_dict7 = {o_k: {i_k: i_v for i_k, i_v in o_v.items()} for o_k, o_v in dict7.items() if 'property' in o_k} print("7:", new_dict7) --

, чтобы оставлять комментарии