Intermediate Python Tutorial #3 - Map() Function

Tech With Tim · Beginner ·🛠️ AI Tools & Apps ·7y ago

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 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

The map() function in Python applies a given function to each item of an iterable (such as a list) and returns a list of the results. This video tutorial demonstrates how to use the map() function and provides an alternative method using list comprehensions.

Key Takeaways
  1. Define a function to be applied to a list
  2. Create a list of values
  3. Apply the map() function to the list
  4. Use list comprehensions as an alternative method
  5. Modify the list using the map() function or list comprehensions
💡 The map() function provides a concise way to apply a function to each item of a list, while list comprehensions offer an alternative method for modifying lists.

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →