Python dictionary comprehension | Python dictionary comprehension examples

preview_player
Показать описание
#PythonDictionaryComprehension

What is Dictionary Comprehension?
================================
Dictionary comprehension is a way to create a Python dictionary using a for loop in a single statement.

What is the use of Dictionary comprehension in Python?
===============================================
Dictionary comprehension is a Pythonic way of creating a dictionary in a loop. It uses the minimum code for creating a dictionary in Python.

When to use dictionary comprehension in Python
===========================================
Dictionary comprehension should be ideally used for creating a dictionary inside a for loop when the conditions for creating the dictionary are limited.

If you have a multi-line condition that should be satisfied for creating a dictionary, then comprehension might not be the best choice for creating a dictionary in Python.

Homework for creating a dictionary using Python comprehension
========================================================
Towards the end of this video, you will find the homework that you could try on your own and create a dictionary of Months and their respective numbers. To get further details please watch the video till the end.

✅ OTHER LINKS AND RESOURCES
=============================
🔴 Subscribe to this channel

🔴 YouTube Channel

🔴 Interviews related to job and work-life in Germany

🔴 My Facebook Page

🔴 Follow me on Twitter

🔴 Follow me on Instagram

🔴 Connect with me on LinkedIn
Рекомендации по теме
Комментарии
Автор

list1 = ["jan", "feb", "mar", "apr", "may", "june", "july", "aug", "sept", "oct", "nov", "dec"]
list2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

dict_item = {item: list2[counter] for counter, item in enumerate(list1) if counter % 2 == 0}
print(dict_item)

xxcosmicgamingxx