Dictionary Comprehension - Create Complex Data Structures Step by Step

preview_player
Показать описание
Welcome to the best tutorial I've ever filmed!!! 😍😍😍
Today we will talk about Dictionary Comprehension which is a very clever technique to construct dictionaries from different collections of data.
We will start with a 🐍 basic code example 🐍 (00:47 - 03:47) and we will move on to practice our new set of skills with 3 FUN and USEFUL exercises:
1. Superheroes exercise:
correct a distorted dictionary (03:47)
2. Genetics exercise:
create a random DNA sequence by pairing bases (08:26)
3. Authentication exercise:
create a dictionary of users with random passwords (14:55)

⭐ Get complete tutorial code ⭐

🎥 RELATED TUTORIALS 🎥
---------------------------------------------
⭐ Python For Loops for Beginners:
⭐ List Comprehension:

⏰ Time Stamps ⏰
--------------------------------
00:00 - intro
00:21 - tutorial plan
00:47 - create dictionary with for loops
01:04 - zip function approach
01:37 - range function approach
02:05 - dictionary comprehension with zip
02:47 - dictionary comprehension with range
03:17 - dictionary comprehension with tuples
03:27 - dictionary comprehension with Pandas Data Frames
03:47 - modify dictionary in place (exercise #1 - superheroes)
08:26 - create a dictionary of lists (exercise #2 - DNA)
14:55 - create a list of dictionaries (exercise #3 - user authentication)
17:23 - create random passwords
20:46 - dictionary and list comprehension disadvantages
21:12 - outro and thanks for watching! :)

💻 Copy starter code 💻
----------------------------------------
⭐ EXERCISE 1:
my_dict = {
"Spider": "photographer",
"Bat": "philanthropist",
"Wonder Wo": "nurse"
}

⭐ EXERCISE 3:
keys = ["id", "username", "password"]
users = ["mariyasha888", "KnotAbot", "spongiBOBO", "IAMBATMAN"]

🤝 Connect with me 🤝
--------------------------------------
🔗 Github:
🔗 Discord:
🔗 LinkedIn:
🔗 Twitter:
🔗 Blog:

💳 Credits 💳
----------------------
⭐ Beautiful icons by:
⭐ Beautiful animated graphics by:
Рекомендации по теме
Комментарии
Автор

In the DNA example, to simplify the code, I would create a dictionary:
pair = {"A":"T", "C":"G", "G":"C", "T":"A"}
Then, instead of the nested if statements, do
dna = {key:[val, pair[val]] for (key, val) in enumerate(strand1)}

katbryce
Автор

The level of polish of the channel in general is really appreciated, thanks for sharing your knowledge !

woled
Автор

"Let's say that Spiderman is not exactly welcome in this DC dictionary" haha that was funny. I loved the examples.

victorj.alonzo
Автор

I feel like dictionary comprehensions lose a little bit of its readability compared to a conventional loop once you start applying multiple conditions. Especially with group else statements rather than the traditional elif which (as you mentioned) doesn't exists within the comprehension.

But really nice to know about the feature as it never occurred to me that this existed.

GOTHICforLIFE
Автор

The 'key' to learn dictionary comprehension is to practice this 'value'able exercises in this video.

tomislam
Автор

Excellent work! The clarity and didactics with which you have explained each concept are impressive. You have managed to break down complex topics into simple and accessible explanations, which is essential for deep understanding. Your ability to maintain simplicity without losing the essence of the concepts is truly admirable. Keep it up! Congratulations..

josueporrasguajard
Автор

I'm using this to teach a student about dictionaries, thank you!

tortugatortuga
Автор

First time it was overwhelming for me, but second time it was fun and I could understand it 100%. I need to implement this in real project before I forget it, Thanks a lot.

sachindeshpande
Автор

Mariya, you can turn the password generator into a function call. And I also added a UID function as well. Great tutorial though. Thanks so much!

davidpimental
Автор

My intuition tells me that range approach is faster, because zip approach must contain the procedure to compare between names and profs. Im a newbie for Python programming. Thank you for your videos, that help me a lot to learn Python.

takeshih.
Автор

Many thanks for the great content. This should be archived for future data analysts and pythonistas.

mikepenprogrammer
Автор

lists/dicts comprehension are constantly on that line where they are either great and easy to make and read or just long and ugly and make it harder to read so sometimes they are great, but sometimes it´s just better to do normal for loops

adrianrayon
Автор

Thank you for this really good video. Definetly the last example was "WOW. I wouldnt be able to do it". Nice !!!!

isojama
Автор

If it's just a simple code, go and use list/dict comprehension, but when you logic become more complex, you better write it the old way, it's lot easier to read. Just like lambda for simple function and not for complex function.

greisboy
Автор

Great video, it is too clear.

A little variation of your code for the exercise #3:

users = ["mariyasha888", "KnotABot", "SpongiBOBO", "IAMBATMAN"]
keys = {
"id": lambda id: id,
"username": lambda username: users[username],
"password": lambda passw: "".join(random.choices(string.printable, k=8))
}
data = [{key:(val(i)) for (key, val) in keys.items()} for i in range(len(users))]

Maybe it's not the best way to do it, but it's just for practice 🙃. I'm learning python 😅. and your videos are very useful!!!

MrDiecrew
Автор

This was a really good tutorial showing more uses for comprehensions. Thanks!
Also, the addition of the slides showing the important notes was a really good add. That was very welcome.
Thanks again!

patricioa
Автор

Thanks. Great video! I haven’t used dictionaries much yet, but I’ll come back to this when I do.

timthompson
Автор

The most complete tutorial on DC. Can you please sware more on that

rgAlex
Автор

I'd expect the "zip" method to be the faster one because it was purpose-made for such cases
and probably has some built-in optimizations, no?
Also the list comprehension where we add "man" works without putting (key, val) in brackets.

flioink
Автор

Requesting for a data structures and algorithm playlist

rst