Change language

Combining dictionaries, the Python 3.10 way – A Python short by Michael Kennedy

Combining dictionaries, the Python 3.10 way - A Python short by Michael Kennedy

Hey there, Im Michael. Welcome to our Python Shorts series. In this series were learning things to do in Python tips, language tips or even frameworks in a short amount of time three minutes, five minutes, something like that. This video were going to be learning about merging dictionaries and how that there is a simple and straightforward way to do it, but its not really the right way in Python, so were going to teach you two ways that are more Pythonic one that works in Python3 in general, and one thats brand new and totally awesome. Lets jump over here to PyCharm and look at some code, so weve got this code, youll be able to access it after the video weve got three dictionaries in our scenario were imagining these are coming from the web this is very common in web frameworks in python like Flask or Django or whatever some data is being passed through the query string. Thats the question mark in the end of the URL some is being posted up as part of the form and then some is actually in the core part of the URL as well. So.

weve got a query post in route. We just want to know what data did they give us? what happened? what have we received? So what were going to do is assume that we dont know Python super well and were just going to go and write some code to loop over that and merge these all together So heres our non-procedural way we have a destination dictionary were calling merged and were going to say, well lets go through the query first and were going to take whatever key it is id render fast and were going to give it the value in this case 1 and True and then were going to do that for post and build that up with merge and do that on a route and build that up with merge and we did this little print statement here at the end the f strings now this cool syntax, we can say value equals and itll do a debug statement so thats going to print the value well show just to remind you what they were and then what the result was. Lets run that all right here it is again straight from the top but now weve got our merged dictionary yes it worked exactly as we hoped a couple of things that are interesting to note. One is that we have id in two places, but we used the last one we said we want to give route higher priority than query string which we probably should, so thats what were passing there and that that worked, but we got all the values combined and when there was duplication. We had the sort of priority fallback. Great, this works totally well this is not very much the way Python expects. Lets go write a little bit more code Im going to come down here and were going to have V2 Pythonic lets say V3 so this only works in python Python3, maybe well call it py3 so its more obvious and were gonna go and run this now, of course its just the same right now but lets change it over here theres a cool Pythonic way Python3 Pythonic way what we can do is if we have multiple dictionaries, we can use the equivalent of the keyword argument style, we can say ** and then the dictionary so we can say query **post, **route now this only works in Python3 hence the little name Ive given it, we dont need this all right and lets actually print out instead of merge by looping. Were going to print out merged equals that and then see the value, so if we run this one notice were running V2. yeah look at that same inputs but now same outputs but much shorter, much cleaner we dont have to write multiple loops in those kind of things and again the order in which we did it query, then route, still got the same override when there was duplicate key so the last one wins in this sense, so this is the Pythonic right way to merge dictionaries in Python3. But wait, were not done. Were not actually done. Check this out one more thing Python lets say V3 and this is going to be Python 310 or above. They just introduced new syntax for us to do this, so lets say 3.10 in other things like sets, weve been able to say like s1 pipe s2 to merge them, youve not been able to do that with dictionaries until python 310. so check this out, well just say get rid of all this. Were just going to say merged is which one do you want to have the lowest priority put that first merge together intersect with or union with rather post and then route and lets just print out that thats the way here right one more time boom same output, oh wait I havent proved anything. We ran the wrong one ran the old one. There we go ran the new one and again same output so you can see now python 310, were running 310 down here arent we? Python 310 introduced this new way with operators just the vertical pipe command instead of the more complex star star kw arg style I like it. It does require you the new Python 310, but here it is and you can use it in your code as long as you can depend on having the latest. If not then fall back to this style, theres really no reason unless youre in a very odd dynamic situation to do the loops merging dictionaries totally easy but make sure you do it the Pythonic ideally, if possible, the Python 310 Pythonic way of doing things. I hope you enjoyed learning how to merge dictionaries in Python if youre looking for more short tips or even longer tips for Python. Be sure to click on my face up here so you can subscribe and if you enjoyed this video if you found it to be useful please please please press the like button down below thatll let YouTube know that it should recommend it to other people. Itll, really help support our work also if you havent found the Talk Python to me podcast. Be sure to check it out over at talkpython.fm and we have hundreds of hours of deeper courses at training.talkpython.fm where you can dive into this stuff now, well go way deep over there, so if you want to go deeper check out the training site if you want more of the shorts, just be sure to subscribe here in YouTube. Thank you. See you next time.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically