Dictionary Comprehension - Create Complex Data Structures Step by Step

Поделиться
HTML-код
  • Опубликовано: 10 сен 2024

Комментарии • 229

  • @katbryce
    @katbryce 2 года назад +54

    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)}

    • @JorgeEscobarMX
      @JorgeEscobarMX 2 года назад +3

      Agree, I felt a little excesive use of ternerary operatrors.

    • @colsadventures
      @colsadventures 2 года назад +1

      I think it should be .. pair[val]] .. but could be wrong.

    • @katbryce
      @katbryce 2 года назад +1

      @@colsadventures Yes it should, I've edited it now. 😳

    • @PythonSimplified
      @PythonSimplified  2 года назад +13

      Wow!! This looks awesome Katrina!! Great job! I love it! 😃😃😃

    • @aldolunabueno2634
      @aldolunabueno2634 Год назад

      I've done it in a similar way:
      dna = {idx:[b, {'A':'T', 'T':'A', 'C':'G', 'G':'C'}[b]] for (idx, b) in enumerate(strand1)}

  • @owoled282
    @owoled282 Год назад +3

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

  • @tomislam
    @tomislam 2 года назад +19

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

    • @PythonSimplified
      @PythonSimplified  2 года назад +1

      hahahaha that was very poetic, Tom!! 😁

    • @tomislam
      @tomislam 2 года назад

      @@PythonSimplified Thank You 😊

  • @tortugatortuga7505
    @tortugatortuga7505 2 года назад +2

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

  • @GOTHICforLIFE1
    @GOTHICforLIFE1 2 года назад +8

    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.

    • @PythonSimplified
      @PythonSimplified  2 года назад +1

      I absolutely agree! I find that Comprehensions are much more comprehendible with lists hahahaha when it comes to dictionaries - the syntax becomes un-Pythonic very fast 😅
      With that said, I use it way more often than traditional loops. I think I trained my brain to actually find it intuitive 🙃 hahahaha

    • @Sinke_100
      @Sinke_100 2 года назад

      @@PythonSimplified even if it looks less redable, i so much enjoy using a comprehenssion of everything, agree with you [x:=str(i)+str(i)[-1] for i in range(0,20,10)][1]+'%' 🙂

  • @josueporrasguajard7736
    @josueporrasguajard7736 5 месяцев назад +1

    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..

  • @victorj.alonzo
    @victorj.alonzo 2 года назад +3

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

    • @PythonSimplified
      @PythonSimplified  2 года назад

      hahahaha glad you like my humor Victor! thank you! 😃

    • @factotums
      @factotums 12 дней назад

      @@PythonSimplified I concur. Using pop culture references not only makes things less tedious, but it really helps with comprehension. That's why this is my favorite coding channel. So many tutorials are just a monotonous voice on a bad mic with a screen recording of their IDE.
      Also just wanted to say you're a really good teacher.

  • @MrDie97crew
    @MrDie97crew 2 года назад +2

    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!!!

  • @mikepenprogrammer2652
    @mikepenprogrammer2652 2 года назад +4

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

  • @sachindeshpande1
    @sachindeshpande1 Год назад

    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.

  • @isojama1
    @isojama1 2 года назад +1

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

    • @PythonSimplified
      @PythonSimplified  2 года назад

      Yeyyy!!! Super happy you liked it Danail! 😃😃😃

  • @diwakar_tsn
    @diwakar_tsn 2 года назад +3

    Hey Mariya
    This video will help many💙🙃❤️

  • @wu1484741111
    @wu1484741111 5 месяцев назад

    This real makes fun with dictionary comprehension for doing own private project

  • @Thebytedoc69
    @Thebytedoc69 6 месяцев назад

    Mariya, Thank You for your very informative and energetic videos. Your videos are excellent and flow so well. Keep up the great work.

  • @rst2205
    @rst2205 2 года назад +7

    Requesting for a data structures and algorithm playlist

    • @PythonSimplified
      @PythonSimplified  2 года назад +2

      Will do! I'm waiting to finish my current Algorithms and DS 2 module in university before I can properly cover the topic 😉
      (My final exam is in September, so not much longer to wait 😊)

    • @rst2205
      @rst2205 2 года назад

      @@PythonSimplified All the best for your exams mam!!

    • @realmadwolf
      @realmadwolf 2 года назад

      @@PythonSimplified Great, And all the best for your exams tho you gonna score 10/10 anyways

  • @patricioa5535
    @patricioa5535 2 года назад +2

    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!

    • @PythonSimplified
      @PythonSimplified  2 года назад

      Thank you so much for the incredible feedback, Patricio! Super happy you enjoyed this tutorial! 😃😃😃

  • @_isDev
    @_isDev Год назад

    Amo a esta mujer! Enseña muy bien!

  • @manoharsagunthalla9215
    @manoharsagunthalla9215 4 месяца назад

    Tank you very much for the video . YOur explanations are awesome

  • @Zub1Wun
    @Zub1Wun 2 месяца назад

    Thank you for this video Mariya, it's really helped me in my journey

  • @brianpfaff5994
    @brianpfaff5994 11 месяцев назад +1

    Very well explained! I like your videos. They help me better understand the Python language. Keep up the good work. 👍🙏

  • @rgAlex8386
    @rgAlex8386 9 месяцев назад

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

  • @francismannion7075
    @francismannion7075 3 месяца назад +1

    Wow that was great ,thank you..

  • @takeshih.1349
    @takeshih.1349 2 года назад +2

    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.

    • @PythonSimplified
      @PythonSimplified  2 года назад +1

      Thank you so much for your comment Takeshi! 😃
      I'm planning a live stream in which we will compare the two approaches! my intuition also tells me range() - however, with Python 3, the zip() approach is also very efficient so I'm expecting a very close battle 😉

  • @milo_andrs
    @milo_andrs 5 месяцев назад

    Love the examples, thank you

  • @davidpimental6704
    @davidpimental6704 Год назад

    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!

  • @adrianrayon2834
    @adrianrayon2834 Год назад +1

    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

  • @timthompson468
    @timthompson468 2 года назад +1

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

  • @_mrmark
    @_mrmark 2 года назад

    Maria, thanks for the lessons. Almost everything is clear and without translation. zip is faster

  • @suvimpemel5583
    @suvimpemel5583 2 года назад +2

    As always the examples you choose are always related to my use cases.
    Thanks a lot for the supper intuitive video.

    • @PythonSimplified
      @PythonSimplified  2 года назад +1

      That's awesome!!! Super happy I could telepathically guess the most useful examples! 😁😁😁

  • @martella13
    @martella13 Год назад

    I'm a Mariya simp :) :) lol your videos are an excellent supplement to my college courses! Thank you for all your hard work!

  • @Sol_Naif
    @Sol_Naif Год назад

    A million thanks Maria, your explanation is very good

  • @kostas6915
    @kostas6915 2 года назад +1

    Excellent material and excellent educational exposition! Very well done!

  • @danadasachan7208
    @danadasachan7208 Год назад

    Thank you Mariya. I found this very helpful and easy to understand.

  • @gregap8282
    @gregap8282 2 года назад

    This might be the best python tutorial I've ever found online. Thanks for sharing.

  • @jeffersonbrunheira6160
    @jeffersonbrunheira6160 Год назад +1

    Thanks a lot! This content is very important for advanced python learn. 😃

    • @PythonSimplified
      @PythonSimplified  Год назад

      Thank you so much Jefferson! I'm glad you found it useful! 😊

  • @plurisdesign3210
    @plurisdesign3210 Год назад

    Best tutorial ever

  • @dagmite
    @dagmite Год назад

    Just helped me tackle my dictionary mental block.. Good stuff!

  • @manoharsagunthalla9215
    @manoharsagunthalla9215 4 месяца назад

    It’s really awesome thank you for the video

  • @varunkaniyala6302
    @varunkaniyala6302 Год назад

    I became mad after watching this video. But you are a great tutor. I became a fan of you. Love you and your English. Thank you so much🧐😵‍💫🤒. I request you to please start a complete python tutorial for beginners.

  • @mrpeace422
    @mrpeace422 Год назад

    Thanks a lot ! ❤❤. I was learned something from you. You are also better than my python profesor . U r taught something to me recent days . U r way speech and teaching is amazing itz easy to understand (but some topics are tough ) although u r videos are great 🥰

  • @Kazner0h
    @Kazner0h Год назад

    You are such a skilled teacher! Thank you

  • @manoharsagunthalla9215
    @manoharsagunthalla9215 4 месяца назад

    Will you please explain in separate video about how to read multiple elements from user at time using split function . 2. How to sort a dictionary without converting to tulle

  • @sbel177
    @sbel177 2 года назад +5

    I just discovered your channel and I've enjoyed following along with you. You are a great teacher! Thank you for all your hard work :)

  • @user-wd1os3vx9q
    @user-wd1os3vx9q 2 года назад +1

    Great tutorial I like very much. Thank you Mariya

  • @bejinbejin2744
    @bejinbejin2744 Год назад +1

    your all videos are very very useful 👍

  • @greisboy425
    @greisboy425 2 года назад

    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.

  • @oguzturkyilmaz543
    @oguzturkyilmaz543 2 года назад +1

    Thanks

  • @kieutrungtran1549
    @kieutrungtran1549 Год назад

    You are a great teacher. Smart and beautiful

  • @Owen7768
    @Owen7768 11 месяцев назад

    your explanation is very clear, thank you!

  • @davidkitano5134
    @davidkitano5134 2 года назад +1

    great video!! thank you. You're a super teacher about python, it's easy with you!! . 3:15 I Think zip is faster than len

    • @PythonSimplified
      @PythonSimplified  2 года назад

      Thank you so much for your comment David! We will compare zip() and range() in an upcoming live stream, we'll see exactly which of them beats the other 😉

  • @dogustabiyat
    @dogustabiyat Год назад

    Very Useful Thanks!

  • @baylonyap1113
    @baylonyap1113 Год назад

    Super Enjoyed and Laughing !! 😀😀😀

  • @kris_programmer
    @kris_programmer 2 года назад +2

    Love the examples. Awesome content!

  • @davidsonfong9549
    @davidsonfong9549 2 года назад

    love your channel. Thank you for simplifying such valuable knowledge! Your delivery is on point.

  • @frenamakenson9844
    @frenamakenson9844 8 месяцев назад

    amazing tips, thx for sharing !

  • @OneMilian
    @OneMilian 2 года назад +1

    Thank you so much!!!!

  • @Asoville18
    @Asoville18 Год назад

    This is great. Thanks.

  • @optimus888amicus
    @optimus888amicus Год назад

    Hi, Sunshine ! 🌞😉👌
    Very good !

  • @kentzhou9022
    @kentzhou9022 2 года назад

    Amazing! was looking forward to this video!! thank you thank you!!

  • @chessketeer
    @chessketeer 11 месяцев назад

    Great teaching! Thank you!

  • @markslima1557
    @markslima1557 2 года назад

    I needed this video! Thank you stay awesome!

  • @kaisj7
    @kaisj7 2 года назад +1

    Nice Video THX , I just begin study dictionary

    • @PythonSimplified
      @PythonSimplified  2 года назад +1

      Awesome timing then! 😉
      Best of luck with studying, Kais! 😃

    • @kaisj7
      @kaisj7 2 года назад

      @@PythonSimplified I try my best I very like Python and I love to learn it and U help me really 😄

  • @hasanbaaqeil42
    @hasanbaaqeil42 Год назад

    beautiful... thanks.. .. I like your extra efforts to make these videos enjoyable with nice editing and awsome graphics

  • @hugocesarescajeda5728
    @hugocesarescajeda5728 2 года назад

    Cheers from México 💋

  • @TechieGuy82
    @TechieGuy82 2 года назад +1

    Lovely as always Mariya.
    I would like to point out that using the random module isn't safe for generating passwords. People might use it to generate passwords for their various accounts online. You could mention the secrets module which is advised and secure. Anyway keep up the good work. Love you ❤️

    • @PythonSimplified
      @PythonSimplified  2 года назад +2

      Thank you so much for the awesome tip, TechieGuy! 😃
      Secrets is indeed much more randomized than Random! the only issue is - I couldn't find a secrets.choices() method but rather secrets.choice() which returns a single value instead of k of them.
      From my perspective, the last exercise seemed convoluted enough to avoid the additional list comprehension of:
      password = "".join([secrets.choice(string.printable) for i in range(8)])
      That's why I went for Random instead... however - if this was a dedicated video for random passwords I'd definitely use Secrets instead 😉

  • @jeuxmathinfo9578
    @jeuxmathinfo9578 Год назад

    Very useful ! 🏆🏆🏆🏅🏅🏅Thank you very much !!! 🙏🤗

  • @Mr_Dean77
    @Mr_Dean77 Год назад +1

    I feel the comprehension with the range function is faster🙃🙃.. cos range functions has less work load to perform than the Zip method.🤔

  • @user-hl4od7cr2i
    @user-hl4od7cr2i 3 месяца назад

    12:55did u just said "neta" ?? xD awesome tutorial!

  • @Anarky35
    @Anarky35 2 года назад

    Awesome video, I've learnt a lot!! Thank you so much

  • @rahulkmail
    @rahulkmail 2 года назад

    Excellent.

  • @QuarktaschemitSenf
    @QuarktaschemitSenf 10 месяцев назад

    1:07 why not use the dict constructor here instead of the loop?
    dict_names_professions = dict(zip(list_names, list_professions))
    any differences , benefits from using the loop?

  • @guimoncao
    @guimoncao 2 года назад +1

    I'm not a Python programmer, but I like your videos

    • @PythonSimplified
      @PythonSimplified  2 года назад +1

      Thank you so much Guilherme!! 😃 I might end up convincing you to start using Python... I have some really exciting projects in the making 😉

    • @guimoncao
      @guimoncao 2 года назад

      @@PythonSimplified Maybe I can use it in my projects too

  • @StamosTee
    @StamosTee 2 года назад +1

    This was an awesome tutorial 👍👍👍

  • @flioink
    @flioink 2 года назад

    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.

  • @papinpython6103
    @papinpython6103 2 года назад +1

    Trop forte cette merveilleuse dame

  • @niftyquizforfun6324
    @niftyquizforfun6324 2 года назад +1

    hello i m from indian yours warm well come i saw yours python video are very great i want learn python i think always may be one day i will learn python but money fees problem but my dream that learn python bcoz i saw python code is so lovely and easy for write and understand yes sue we can do with python code many great thing so my simple and hamble request from you can you teach to me python step by step from beginning thanq

  • @goldie2799
    @goldie2799 2 года назад +1

    Realy good video thanks a lot !

  • @sumitdutta7043
    @sumitdutta7043 Год назад

    Thanks 😊

  • @cdxc_55uh
    @cdxc_55uh Год назад

    Wow, what a nice RUclips channel. Nice to see you, Mariya!

  • @taylormccoy7492
    @taylormccoy7492 Год назад

    I think it would probably be faster to use the zip method. Two primary reasons, one being that with the range approach you have to check the list on each iteration, and two being since the zip function was probably programmed to do this efficiently it has a different method of accessing the objects. Just my guess!

  • @dimitriosdesmos4699
    @dimitriosdesmos4699 2 года назад

    very good very clever, very helpful.....but i rarely use these comprehensions and it would be faster for me to do it the long way than sit down to memorise the rules...then forget them.

  • @jongcheulkim7284
    @jongcheulkim7284 Год назад

    Thank you^^ This is very helpful.

  • @todayinhistory965
    @todayinhistory965 2 года назад +1

    Wow, thanks dear

  • @nayanjadhav7841
    @nayanjadhav7841 Год назад

    Wonderful video.

  • @codewithbheem
    @codewithbheem 2 года назад

    Happy teacher day mam

  • @darta1094
    @darta1094 2 года назад +1

    Password generation code (in my tests) may produce a string with one or more spaces. Used replace to get rid of those.

    • @PythonSimplified
      @PythonSimplified  2 года назад

      That was my original plan, Dimitri! 😃but then I thought to myself - what's the problem with spaces? I couldn't find a single reason why they would be any different from the rest of the characters 🤷‍♀️
      Some sites do prevent you from using spaces within passwords - but I don't think it's based on merit. Do you find that space-less passwords are more secure?

    • @Sinke_100
      @Sinke_100 2 года назад

      Because string.printable contains whitespace \t,
      ,
      , at first i used to cut it at the end when i imported it, but for me better aproach was change the original string module and create printable_ww same but without white space, i would like to contribute to open source to change that for python 3.12 if comunity accepts it

  • @jamvin5647
    @jamvin5647 Год назад

    3:15 I think the zip approach is faster because the syntax is more comprehensive

  • @Knowledgeseeker7101
    @Knowledgeseeker7101 Год назад

    please more video ...❤❤❤❤❤

  • @georgeorwell2334
    @georgeorwell2334 2 года назад +1

    NIce to see again - greets from Germany.

    • @PythonSimplified
      @PythonSimplified  2 года назад

      Thank you so much George Orwell! Huge fan of your 1984 book 😉 hahaha
      Greetings from Vancouver! 😃

  • @semlion4972
    @semlion4972 2 года назад +1

    Thanks for another video if you can in the future make python with MySQL some mini project tnx. 😀

    • @PythonSimplified
      @PythonSimplified  2 года назад

      Hi Sem 😃
      MySQL is definitely on my to-do list! will cover it in my Linux machine in future tutorials. In the meanwhile, you can checkout my SQLite tutorials:
      ⭐ SQLite Backend for Beginners:
      ruclips.net/video/Ohj-CqALrwk/видео.html
      (please note - the above tutorial is missing connection.commit())
      ⭐Web Scraping Databases with Mechanical Soup and Sqlite:
      ruclips.net/video/MkGQmZoMuRM/видео.html
      (includes connection.commit() which is very important!)
      It's not exactly MySQL, but it's right off the same alley 😉

  • @devvsakib5223
    @devvsakib5223 2 года назад +1

    Great!

  • @QuarktaschemitSenf
    @QuarktaschemitSenf 10 месяцев назад

    10:23 shouldnt it be "strand1" inside the () brackets instead of dna_st1 ? sorry im easy to confuse xD. Also in the last line of the for-loop it should be b2 = "C" otherwise you can pair A with G

  • @aasifkhan1545
    @aasifkhan1545 2 года назад +1

    I think the range approach is fastest.

  • @khalidmohamed6373
    @khalidmohamed6373 Год назад

    well explained

  • @tomknud
    @tomknud 2 года назад

    Indeed your best! ♥

  • @shinrafahell
    @shinrafahell 2 года назад +1

    Thank you beautiful smart lady!

  • @stop_the_fraud5474
    @stop_the_fraud5474 Год назад

    ..great job Mariya. )) Which university did you attend that you reference at times? Do you teach somewhere? Again thanks. )))

  • @jackroark6928
    @jackroark6928 Год назад

    🍓⭐high quality

  • @Demandd
    @Demandd Год назад

    Why are some of the passwords that are randomly generated more than 8 characters even though we used k=8?

  • @sudhansumtripathy
    @sudhansumtripathy Год назад

    really got stuck how we can put multiple values in the value ie [ value, if statemen], if you could further elaborate