Intermediate Python Tutorial #3 - Map() Function
Skills:
Python for Data80%
Key Takeaways
The video tutorial covers the map() function in Python, demonstrating its use in applying a function to a list and creating a new list with the returned values. It also explores list comprehensions as an alternative method.
Full Transcript
hey guys and welcome back to another YouTube video in today's video this is the third video in our intermediate Python tutorials and I'm gonna be going over the map function so pretty much the map function is an extremely useful tool that allows us to apply a function to a list and then create a new list with those new values and you'll understand what I mean as I go through an example here so before I even start using the map function I want to just present us with a problem the map function consult and this is kind of the best way to understand it so I'm gonna create a list which we call it Li of integers up to 10 like this so 9 9 10 ok and now I'm going to create a function and I'm gonna call it funk and it's simply gonna take one parameter X and all we're going to do is we're gonna return X to the exponent X very simple okay now what this problem is it's very simple um I want you or I want to be able to apply this function X to every value in the list here so 1 to 10 and then have that stored in a new list so intuitively the way that you would want to do this or the way that you would try to do this at least is you make something like a list is you go to this you'd write a for loop say for X in Li and then you say new list dot append and then you would put well funk and X like that and then if you printed out your new list you would get that value I don't know why print just showing up in a different color now but anyways and there we go so we get one for 27 and so on and so forth ok so now what we want to do is use the map function to do this faster so this is a very valid way to do this this works fine but I can actually shorten all four of these lines into one by using the map function so let's go ahead and do that now so I'm going to start by typing it uh actually we're just going to print because I want to print it first starting with I'm gonna do list like that and I'll tell you why in a second I'm going to my map function like so and now your map function takes two arguments so it takes a function and it takes a list so let's give it our function which in this case is gonna be called func and then let's give it our list so I actually don't need these double brackets here and excuse me let's give it our list which in this case is Li so let's talk about what this really is doing right now so the map function takes a function which is funk so the name of our function and it takes a list which is Li what it's going to do is it's going to apply this function to every element in the list and we can watch this happen we run the program and you see we get the exact same value as we did before and that's extremely extremely useful in Python because there's a lot of times you want to apply a function or even possibly multiple functions onto a very onto a list or every element of the list and you don't have to type out a whole for loop yes you can do that it works fine but the map function is just a shortcut to be able to do that so let me just show you now maybe a few other examples that we can use so like using this map function there's another way that we can create this and it's totally preference which one you want to use I like using map just because it's kind of cool you can also use wink a list comprehension and this is something I would have shown in some of my previous Python tutorials like from way back like years ago on my channel but I'll go over it quickly now and the way that this works is you can do the same things you can say func X for X in Li now this is actually going to give us the exact same value as what we have up here with this map function Li for example because what we're going to do is we're going to say we're gonna take this right here so whatever this expression is and we're gonna do it for every value of x in the list so that's exactly what this map function does and this is gonna turn it into a list because we put our square brackets here so I print this out see we get again the same value and that's how list comprehension works now for a list comprehension you can also add like an expression anywhere so it can do something like if let's say X modulus which modulus 2 equals equal to 0 and now it's only gonna do this we're put this into the list if X is divisible by 2 syntax error oh I forgot my square bracket here my bad don't know how you even got rid of that that's friendly oh that's cuz that is why all right excuse me on that okay so now we get 4 2 5 6 so it's only doing it for the even numbers of X so yeah those are two really cool ways to kind of change a list or modify them apply a function to them in Python say obviously you might want to use a function that's more advanced than this this is just for a basic example and if you wanted to apply maybe two functions to it you could have X to be changed here and then you could return like function two of X to the X here and then that would put that value obviously in the map so again map takes two parameters a function and a list and it will apply the function to every value in that list I hope this has been useful for you guys if you did enjoy the video please make sure you leave a like and subscribe and I'm going to be moving on to some more complicated and other examples using map and other functions in the next video so make sure you stay tuned for that [Music]
Original Description
Welcome to a new series! Intermediate Python Tutorials!
Today's Topic: map() function, this function takes two arguments: a function and a list. It then applies the function to all of the values of the list of and creates a new values with the returned values from thew function.
In this set of videos I will be explaining more advanced programming concepts and showing you intermediate-advanced tools that you can use in python. A lot of these tools will help you to solve problems/code more efficiently and will save you a ton of time!
Text-Based Tutorial: https://techwithtim.net/tutorials/python-programming/intermediate-python-tutorials/map-function/
Twitter: https://twitter.com/TechWithTimm
Future topics:
4. Filter Function
5. Lambda Functions
6. Intro to Collections
7. Collections: named tuple
8. Collections: deque
9. Collections: orderedDict
10. Collections: defualtDict
11. docstrings
Want To Support This Channel?
Bitcoin: 1PbkAYLFaJBgjbKn2ptGyBz65xWN8hJgBU
Ethereum: 0xdd42dbbdba60f7163fc7a840e189474b6e8bfcad
Ripple: rD4arM9CVjQWqi8f1kxdpCgkCgEkqBgtud
Please leave a LIKE and SUBSCRIBE for more content!
Tags:
- Tech
- Tech With Tim
- Crypto
- Programming
- Coding
- Pygame
- Python Tutorials
- Intermidiate Python Tutorials
- map function in python
- learn to code
- map()
- how to use map() python
- how to use map()
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: Python for Data
View skill →
🎓
Tutor Explanation
DeepCamp AI