A Clever Way To Use Sets In Python
Video Title: A Clever Way To Use Sets In Python
Hey everyone here we have a coding challenge that we’re going to be working through in today’s video so we need to write a function called merge arrays that takes two lists of integers as inputs combines them removes the duplicates and sorts the combined list and returns it so I have a series of
Tests down here that’ll call our function to test to see if we passed or not okay so let’s start off by defining our function so let’s call it merge arrays and we’ll have array a and array b as our input variables so in here the first thing we’re going
To do is just combine our two input arrays at first so let’s define a new variable called array C and just set that equal to array a plus array B okay so now that we’ve got a new combined list we need to remove any duplicates so
One quick trick for doing this in Python is to just turn this array C into a set sets by Nature remove any duplicates due to being stored in a hash table I recommend checking out my video on why this happens to learn learn more okay so
Let’s do array D is equal to set with array c as the input and then once it’s turned to a set all the duplicates are removed let’s then turn it back into a list so at this point we’ve combined the two input lists we’ve removed the
Duplicates by turning it into a set and then we’ve turn it back into a list now we need to sort the list so now let’s do array e is equal to sorted with array d as the input so this sorted function is a built-in fun function in Python it
Returns a new list containing all the items from an iterable like list tles strings or sets and sorts them in ascending order okay so now that we’ve sorted the list now we can return array e all right so I think we’re done so let’s run our tests and there we go we
Got all passed all right so you can probably see we can actually clean this function up quite a bit I can actually combine a bunch of these steps so let me just erase all of this and now let’s do array AR a plus array B and let’s turn
This into a set immediately when they’re combined then once it’s turned into a set let’s then use the sorted function to sort this set now you may be asking wait shouldn’t I turn this set into a list first well remember the sorted function takes any iterable like this
Set and returns a sorted list in ascending order so there’s really no need to turn it into a list before using this sorted function because the sorted function is going to do it for me okay and that’s it now we can just return this oneliner so let’s run our test
Again and there we go hopefully you enjoyed working through this challenge with me and maybe you learned something new if you did be sure to drop a like I encourage you to think of other solutions to this challenge I’ve added the code to my GitHub link below if you
Wanted to try it out yourself thanks for watching and I’ll see you all in the next video
Video Keywords: Python, python,programming,tech,software,engineering,program,technology,coding,code,coder,programmer,engineer,software engineer,computer,computer programming,tips,codelife,computers,function,operators,new,learn python,what is python,how to,python tips,tricks,beginner,tutorial,learn,tip,slice,of,py,slice of py,sets,set,duplicates,duplicate,hash,hash(),hash function,hash table,set(),array,challenge,sorted,sorted(),sort,list,lists
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
I dropped a like even if I didn't learn anything cause it is super well explained 🙂
Tell us your theme!
how did you do the testing?
Not sure which is faster, but a list comprehension one-liner would have also worked
Yo Man, great work. Work is your theme?
What theme did you use?
Very well done and explained!! It really helps that you went step by step with an expanded solution but then ALSO showed the refactored version which is extremely helpful in understanding both ways overall and how you came to it – Thank you so much for doing that!! 😊👏
Great video! Could I suggest a following one with an explanation about the memory addresses of each array. You do have another video about variables, but this is a subject that I really struggle at. Thanks for your work!
Sipmle and awesome 👌
Set automatically sorts itself on init, isnt it?
Niceee
very interesting, didnt knew sorted returns list
I like it, in the end I would it even like it more if you wrote a lambda 🙂
merge_arrays = lambda arrayA, arrayB: sorted(set(arrayA + arrayB))
a = [3,3,3,1,1,1,2,2,2,3,3,3]
b = [4,5,6]
print(merge_arrays(a, b))
Very well done and easy to understand. I appreciate the normalization at the end, Every coder should do this but many don’t.