<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://www.physicsbook.gatech.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Sabrinayang</id>
	<title>Physics Book - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="http://www.physicsbook.gatech.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Sabrinayang"/>
	<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/Special:Contributions/Sabrinayang"/>
	<updated>2026-05-01T03:41:29Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.42.7</generator>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48111</id>
		<title>VPython MapReduceFilter</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48111"/>
		<updated>2026-04-27T01:09:34Z</updated>

		<summary type="html">&lt;p&gt;Sabrinayang: /* Lambda Expressions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;&amp;lt;big&amp;gt;Sabrina Yang - Spring 2026&amp;lt;/big&amp;gt;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Introduction&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; are &lt;br /&gt;
known as higher-order functions. Higher-order functions are functions that &lt;br /&gt;
take other functions as arguments. VPython simulations usually have hundreds of &lt;br /&gt;
objects that all follow the same physics equations, and writing for-loops to do the same &lt;br /&gt;
calculation over and over can be inefficient. &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &lt;br /&gt;
&amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; take care of the for-loops, &lt;br /&gt;
allowing you to focus on the point of the code itself.&lt;br /&gt;
&lt;br /&gt;
Each function does has a different job:&lt;br /&gt;
* &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;: applies a function to every element in a list&lt;br /&gt;
* &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;: iterates through a list and only keeps the elements that meet a certain condition&lt;br /&gt;
* &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;: takes a whole list and turns it into one single value&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Python 3 note:&#039;&#039;&#039; &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; do not execute until you ask for the results. Wrap them in &amp;lt;code&amp;gt;list()&amp;lt;/code&amp;gt; to get &lt;br /&gt;
the results. &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; has to be imported before you can use it:&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; GlowScript does not support lambda expressions or the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. Use regular &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; functions instead of &lt;br /&gt;
lambdas, and write &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; yourself. Here is an example of this &lt;br /&gt;
in the simulation section below.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Background&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, functions are objects, meaning you can:&lt;br /&gt;
* Store a function in a variable&lt;br /&gt;
* Pass a function into another function as an argument&lt;br /&gt;
* Get a function back as a return value&lt;br /&gt;
&lt;br /&gt;
A few reasons why writing code this way is efficient:&lt;br /&gt;
* &#039;&#039;&#039;It is easy to read:&#039;&#039;&#039; You can immediately tell what the program is trying to achieve. There is no complex decoding required.&lt;br /&gt;
* &#039;&#039;&#039;Fewer bugs:&#039;&#039;&#039; Every time you write a for-loop you have to manually keep track of index variables, which is easy to make mistakes. These functions automatically handle the indices, so there is less room for errors.&lt;br /&gt;
* &#039;&#039;&#039;You can chain them:&#039;&#039;&#039; The output of &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; goes straight into &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; without extra steps, which keeps the amount variables needed to a minimum.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Lambda Expressions&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
A &#039;&#039;&#039;lambda expression&#039;&#039;&#039; is a function you write in one line without giving it a name. &lt;br /&gt;
The format is:&lt;br /&gt;
&lt;br /&gt;
 lambda &amp;lt;parameters&amp;gt;: &amp;lt;expression&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function only exists at the point where it is used. These two functions achieve the same output:&lt;br /&gt;
 # Regular way&lt;br /&gt;
 def square(x):&lt;br /&gt;
     return x**2&lt;br /&gt;
 &lt;br /&gt;
 # Lambda way&lt;br /&gt;
 square = lambda x: x**2&lt;br /&gt;
&lt;br /&gt;
Lambdas save you from having to define a whole separate function when you only &lt;br /&gt;
need it once:&lt;br /&gt;
&lt;br /&gt;
 # Without lambda: needs a function defined somewhere else&lt;br /&gt;
 result = list(map(square, [1, 2, 3, 4]))&lt;br /&gt;
 &lt;br /&gt;
 # With lambda: no separate function needed&lt;br /&gt;
 result = list(map(lambda x: x**2, [1, 2, 3, 4]))&lt;br /&gt;
 # Result: [1, 4, 9, 16]&lt;br /&gt;
&lt;br /&gt;
They can also take two inputs, which comes up when using &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 total = reduce(lambda acc, x: acc + x, [1, 2, 3, 4], 0)&lt;br /&gt;
 # Result: 10&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lambda vs def — when to use which:&#039;&#039;&#039;&lt;br /&gt;
* Use a lambda when the function is short, only needed once, and passed right into another function&lt;br /&gt;
* Use &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when the function is longer or used in multiple places&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; Lambdas do not work in GlowScript. Always use &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when writing code for Trinket.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Inputs and Type Matching&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
All three functions follow the same basic structure:&lt;br /&gt;
&lt;br /&gt;
 function_name(function, list)&lt;br /&gt;
&lt;br /&gt;
The first argument is the function you want to use. The second is the list of &lt;br /&gt;
data you want to run it on, which can be a list, tuple, etc.&lt;br /&gt;
&lt;br /&gt;
One thing to watch out for: the function has to be able to work with any item &lt;br /&gt;
is in the list. If your list has decimal numbers but your function expects &lt;br /&gt;
whole numbers, Python will throw an error.&lt;br /&gt;
&lt;br /&gt;
Example using a named function:&lt;br /&gt;
 def cubed(x):&lt;br /&gt;
     return x**3&lt;br /&gt;
 &lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(cubed, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
The same thing with a lambda:&lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(lambda x: x**3, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
You can also pass Python&#039;s built-in functions straight in:&lt;br /&gt;
 words = [&#039;hello&#039;, &#039;world&#039;, &#039;vpython&#039;]&lt;br /&gt;
 lengths = list(map(len, words))&lt;br /&gt;
 # Result: [5, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Map()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;map(function, list)&amp;lt;/code&amp;gt; runs a function on every single element in a list &lt;br /&gt;
and gives you back all the results. The original list stays the same; you get a &lt;br /&gt;
new list of updated values.&lt;br /&gt;
&lt;br /&gt;
 map(function, list)&lt;br /&gt;
&lt;br /&gt;
So for a list &amp;lt;code&amp;gt;[a, b, c, d]&amp;lt;/code&amp;gt; and some function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;:&lt;br /&gt;
 map(f, [a, b, c, d])  →  [f(a), f(b), f(c), f(d)]&lt;br /&gt;
&lt;br /&gt;
=== Basic example ===&lt;br /&gt;
&lt;br /&gt;
 numlist = [1, 2, 3, 4, 5]&lt;br /&gt;
 result = list(map(lambda x: x * 2, numlist))&lt;br /&gt;
 # Result: [2, 4, 6, 8, 10]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational weight (F = mg) for the masses:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8  # m/s^2&lt;br /&gt;
 masses = [0.5, 1.0, 2.5, 5.0, 10.0]  # kg&lt;br /&gt;
 &lt;br /&gt;
 weights = list(map(lambda m: m * g, masses))&lt;br /&gt;
 # Result: [4.9, 9.8, 24.5, 49.0, 98.0]  Newtons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Kinetic energy of the velocities:&#039;&#039;&#039;&lt;br /&gt;
 mass = 2.0   # kg&lt;br /&gt;
 velocities = [3.0, 5.5, 2.1, 8.0]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 ke_list = list(map(lambda v: 0.5 * mass * v**2, velocities))&lt;br /&gt;
 # Result: [9.0, 30.25, 4.41, 64.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Converting temperature from Celsius to Kelvin:&#039;&#039;&#039;&lt;br /&gt;
 temps_C = [0, 20, 37, 100, -273.15]&lt;br /&gt;
 temps_K = list(map(lambda T: T + 273.15, temps_C))&lt;br /&gt;
 # Result: [273.15, 293.15, 310.15, 373.15, 0.0]  Kelvin&lt;br /&gt;
&lt;br /&gt;
=== map() vs a for-loop ===&lt;br /&gt;
&lt;br /&gt;
Both of these give the same answer, but &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; is more concise:&lt;br /&gt;
&lt;br /&gt;
 # For-loop&lt;br /&gt;
 weights = []&lt;br /&gt;
 for m in masses:&lt;br /&gt;
     weights.append(m * 9.8)&lt;br /&gt;
 &lt;br /&gt;
 # map() — same thing in one line&lt;br /&gt;
 weights = list(map(lambda m: m * 9.8, masses))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Filter()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;filter(function, list)&amp;lt;/code&amp;gt; iterates through a list and only keeps the elements &lt;br /&gt;
where the function returns &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 filter(function, list)&lt;br /&gt;
 filter(lambda x: condition, list)&lt;br /&gt;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
 numbers = [3, 7, 5, 2, 1, 6]&lt;br /&gt;
 result = list(filter(lambda x: x &amp;gt; 3, numbers))&lt;br /&gt;
 # Result: [7, 5, 6]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Retrieving only the fastest particles:&#039;&#039;&#039;&lt;br /&gt;
 speeds = [120, 340, 95, 500, 210, 80]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 fast_particles = list(filter(lambda v: v &amp;gt; 200, speeds))&lt;br /&gt;
 # Result: [340, 500, 210]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Keeping the positive charges:&#039;&#039;&#039;&lt;br /&gt;
 charges = [-1.6e-19, 1.6e-19, -3.2e-19, 3.2e-19, 0, 1.6e-19]  # Coulombs&lt;br /&gt;
 &lt;br /&gt;
 positive = list(filter(lambda q: q &amp;gt; 0, charges))&lt;br /&gt;
 # Result: [1.6e-19, 3.2e-19, 1.6e-19]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Removing particles that exited the simulation boundary:&#039;&#039;&#039;&lt;br /&gt;
 # Each particle p has a .pos.x value for its x position&lt;br /&gt;
 boundary = 10.0  # meters&lt;br /&gt;
 &lt;br /&gt;
 inside = list(filter(lambda p: abs(p.pos.x) &amp;lt; boundary, particles))&lt;br /&gt;
&lt;br /&gt;
=== Passing None as the function ===&lt;br /&gt;
&lt;br /&gt;
If you pass &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; instead of a function, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; removes &lt;br /&gt;
every zero, empty string, &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt; from the list:&lt;br /&gt;
&lt;br /&gt;
 messy = [1, 0, 3, None, 5, 0, 7]&lt;br /&gt;
 clean = list(filter(None, messy))&lt;br /&gt;
 # Result: [1, 3, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;reduce(function, list, starting value)&amp;lt;/code&amp;gt; takes a list and returns a single value. &lt;br /&gt;
&lt;br /&gt;
The function you pass in needs to take two inputs:&lt;br /&gt;
* The running total so far&lt;br /&gt;
* The next item in the list&lt;br /&gt;
&lt;br /&gt;
After each step, the result becomes the new total for the next step.&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 reduce(function, list, starting value)&lt;br /&gt;
&lt;br /&gt;
=== Step-by-step walkthrough ===&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 numbers = [1, 2, 3, 4]&lt;br /&gt;
 result = reduce(lambda x, y: x * y, numbers)&lt;br /&gt;
 # Step 1: x=1, y=2  →  1 * 2 = 2&lt;br /&gt;
 # Step 2: x=2, y=3  →  2 * 3 = 6&lt;br /&gt;
 # Step 3: x=6, y=4  →  6 * 4 = 24&lt;br /&gt;
 # Final result: 24&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Adding up all the masses in a system:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 masses = [1.0, 2.0, 3.0, 4.0]  # kg&lt;br /&gt;
 total_mass = reduce(lambda acc, m: acc + m, masses, 0.0)&lt;br /&gt;
 # Result: 10.0 kg&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Finding the fastest particle in a list:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 speeds = [3.2, 7.8, 1.1, 9.4, 5.5]  # m/s&lt;br /&gt;
 max_speed = reduce(lambda a, b: a if a &amp;gt; b else b, speeds)&lt;br /&gt;
 # Result: 9.4 m/s&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Total work done (W = F·d):&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 forces = [10.0, 25.0, 5.0, 40.0]       # Newtons&lt;br /&gt;
 displacements = [2.0, 1.5, 3.0, 0.5]   # meters&lt;br /&gt;
 &lt;br /&gt;
 work_list = list(map(lambda fd: fd[0] * fd[1], zip(forces, displacements)))&lt;br /&gt;
 total_work = reduce(lambda acc, w: acc + w, work_list, 0.0)&lt;br /&gt;
 # Result: 20.0 + 37.5 + 15.0 + 20.0 = 92.5 Joules&lt;br /&gt;
&lt;br /&gt;
=== Watch out ===&lt;br /&gt;
&lt;br /&gt;
Always give &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; a starting value as the third argument. If the &lt;br /&gt;
list is empty and there is no starting value, Python will crash. With a starting &lt;br /&gt;
value, an empty list will give you that value back instead:&lt;br /&gt;
&lt;br /&gt;
 reduce(lambda acc, x: acc + x, [], 0.0)&lt;br /&gt;
 # Returns 0.0 safely&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Combining map(), filter(), and reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
It is extremely efficient that these functions work well when chained together.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example: kinetic energy&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 # Step 1: filter(): drop all values below 3.0 m/s&lt;br /&gt;
 moving = list(filter(lambda p: p.speed &amp;gt; 3.0, particles))&lt;br /&gt;
 &lt;br /&gt;
 # Step 2: map(): calculate the kinetic energy for each remaining particle&lt;br /&gt;
 ke_list = list(map(lambda p: 0.5 * p.mass * p.speed**2, moving))&lt;br /&gt;
 &lt;br /&gt;
 # Step 3: reduce(): add them all up&lt;br /&gt;
 total_ke = reduce(lambda acc, ke: acc + ke, ke_list, 0.0)&lt;br /&gt;
 print(&amp;quot;Total KE of fast particles:&amp;quot;, round(total_ke, 2), &amp;quot;J&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
This filter → map → reduce pattern shows up very commonly physics simulations. &lt;br /&gt;
Pick a group of objects, apply a formula to each one, then get one final number &lt;br /&gt;
out of it. Below are some examples of these functions chained together.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational potential energy at different heights:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8   # m/s^2&lt;br /&gt;
 m = 2.0   # kg&lt;br /&gt;
 heights = [1.0, 5.0, 10.0, 20.0, 50.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 pe_list = list(map(lambda h: m * g * h, heights))&lt;br /&gt;
 # Result: [19.6, 98.0, 196.0, 392.0, 980.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Electric force on an electron at different distances from a charge (Coulomb&#039;s law):&#039;&#039;&#039;&lt;br /&gt;
 k = 8.99e9   # N·m^2/C^2&lt;br /&gt;
 Q = 1.0e-6   # source charge, Coulombs&lt;br /&gt;
 r_list = [0.1, 0.2, 0.5, 1.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 forces = list(map(lambda r: k * Q * 1.6e-19 / r**2, r_list))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Simulation&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
The following GlowScript simulation shows all three functions working together &lt;br /&gt;
in a real-world physics example:&lt;br /&gt;
[https://trinket.io/glowscript/9e53d57b352f map(), filter(), and reduce() in VPython Physics — Trinket]&lt;br /&gt;
&lt;br /&gt;
The simulation has five spheres in a row, each with a different mass between &lt;br /&gt;
1 and 8 kg and a different speed between 1.5 and 6 m/s. The size of each sphere &lt;br /&gt;
matches its mass.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;map()&#039;&#039;&#039; goes through every mass and calculates the weight using F = mg. &lt;br /&gt;
The weight of each sphere gets printed to the console.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;filter()&#039;&#039;&#039; checks each sphere&#039;s speed and keeps the ones faster &lt;br /&gt;
than 3.0 m/s. &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;reduce()&#039;&#039;&#039; is written by hand since GlowScript does not have the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. It adds up the kinetic energy of every sphere &lt;br /&gt;
one by one until it has one total number for the whole system, which then &lt;br /&gt;
gets printed.&lt;br /&gt;
&lt;br /&gt;
This simulation is an accurate visualization of how all three functions work together.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;References&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
[http://www.bogotobogo.com/python/python_fncs_map_filter_reduce.php 1. Python map, filter, reduce — bogotobogo.com]&lt;br /&gt;
&lt;br /&gt;
[http://book.pythontips.com/en/latest/map_filter.html 2. Map, Filter — Python Tips]&lt;br /&gt;
&lt;br /&gt;
[http://vpython.org/contents/docs/VisualIntro.html 3. VPython Documentation — vpython.org]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functions.html 4. Python 3 Built-in Functions (map, filter) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functools.html 5. functools module (reduce) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/howto/functional.html 6. Functional Programming HOWTO — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-map-function/ 7. Python&#039;s map() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-filter-function/ 8. Python&#039;s filter() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-reduce-function/ 9. Python&#039;s reduce() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-lambda/ 10. Lambda Expressions in Python — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/functional-programming-in-python/ 11. Functional Programming in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/higher-order-functions-in-python/ 12. Higher-Order Functions in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.glowscript.org 13. GlowScript VPython — Official Site]&lt;br /&gt;
&lt;br /&gt;
[https://trinket.io/glowscript 14. GlowScript on Trinket]&lt;br /&gt;
&lt;br /&gt;
[https://vpython.org 15. VPython Official Documentation]&lt;/div&gt;</summary>
		<author><name>Sabrinayang</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48110</id>
		<title>VPython MapReduceFilter</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48110"/>
		<updated>2026-04-27T01:08:25Z</updated>

		<summary type="html">&lt;p&gt;Sabrinayang: /* Lambda Expressions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;&amp;lt;big&amp;gt;Sabrina Yang - Spring 2026&amp;lt;/big&amp;gt;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Introduction&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; are &lt;br /&gt;
known as higher-order functions. Higher-order functions are functions that &lt;br /&gt;
take other functions as arguments. VPython simulations usually have hundreds of &lt;br /&gt;
objects that all follow the same physics equations, and writing for-loops to do the same &lt;br /&gt;
calculation over and over can be inefficient. &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &lt;br /&gt;
&amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; take care of the for-loops, &lt;br /&gt;
allowing you to focus on the point of the code itself.&lt;br /&gt;
&lt;br /&gt;
Each function does has a different job:&lt;br /&gt;
* &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;: applies a function to every element in a list&lt;br /&gt;
* &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;: iterates through a list and only keeps the elements that meet a certain condition&lt;br /&gt;
* &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;: takes a whole list and turns it into one single value&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Python 3 note:&#039;&#039;&#039; &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; do not execute until you ask for the results. Wrap them in &amp;lt;code&amp;gt;list()&amp;lt;/code&amp;gt; to get &lt;br /&gt;
the results. &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; has to be imported before you can use it:&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; GlowScript does not support lambda expressions or the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. Use regular &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; functions instead of &lt;br /&gt;
lambdas, and write &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; yourself. Here is an example of this &lt;br /&gt;
in the simulation section below.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Background&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, functions are objects, meaning you can:&lt;br /&gt;
* Store a function in a variable&lt;br /&gt;
* Pass a function into another function as an argument&lt;br /&gt;
* Get a function back as a return value&lt;br /&gt;
&lt;br /&gt;
A few reasons why writing code this way is efficient:&lt;br /&gt;
* &#039;&#039;&#039;It is easy to read:&#039;&#039;&#039; You can immediately tell what the program is trying to achieve. There is no complex decoding required.&lt;br /&gt;
* &#039;&#039;&#039;Fewer bugs:&#039;&#039;&#039; Every time you write a for-loop you have to manually keep track of index variables, which is easy to make mistakes. These functions automatically handle the indices, so there is less room for errors.&lt;br /&gt;
* &#039;&#039;&#039;You can chain them:&#039;&#039;&#039; The output of &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; goes straight into &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; without extra steps, which keeps the amount variables needed to a minimum.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Lambda Expressions&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
A &#039;&#039;&#039;lambda expression&#039;&#039;&#039; is a function you write in one line without giving it a name. &lt;br /&gt;
The format is:&lt;br /&gt;
&lt;br /&gt;
 lambda &amp;lt;parameters&amp;gt;: &amp;lt;expression&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function only exists at the point where it is used. These two functions achieve the same output:&lt;br /&gt;
 # Regular way&lt;br /&gt;
 def square(x):&lt;br /&gt;
     return x**2&lt;br /&gt;
 &lt;br /&gt;
 # Lambda way&lt;br /&gt;
 square = lambda x: x**2&lt;br /&gt;
&lt;br /&gt;
Lambdas save you from having to define a whole separate function when you only &lt;br /&gt;
need it once:&lt;br /&gt;
&lt;br /&gt;
 # Without lambda: needs a function defined somewhere else&lt;br /&gt;
 result = list(map(square, [1, 2, 3, 4]))&lt;br /&gt;
 &lt;br /&gt;
 # With lambda: no separate function needed&lt;br /&gt;
 result = list(map(lambda x: x**2, [1, 2, 3, 4]))&lt;br /&gt;
 # Result: [1, 4, 9, 16]&lt;br /&gt;
&lt;br /&gt;
They can also take two inputs, which comes up when using &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 total = reduce(lambda acc, x: acc + x, [1, 2, 3, 4], 0)&lt;br /&gt;
 # Result: 10&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lambda vs def — when to use which:&#039;&#039;&#039;&lt;br /&gt;
* Use a lambda when the function is short, only needed once, and passed right &lt;br /&gt;
into another function&lt;br /&gt;
* Use &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when the function is longer or used in multiple places&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; Lambdas do not work in GlowScript at all. Always use &lt;br /&gt;
&amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when writing code for Trinket.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Inputs and Type Matching&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
All three functions follow the same basic structure:&lt;br /&gt;
&lt;br /&gt;
 function_name(function, list)&lt;br /&gt;
&lt;br /&gt;
The first argument is the function you want to use. The second is the list of &lt;br /&gt;
data you want to run it on, which can be a list, tuple, etc.&lt;br /&gt;
&lt;br /&gt;
One thing to watch out for: the function has to be able to work with any item &lt;br /&gt;
is in the list. If your list has decimal numbers but your function expects &lt;br /&gt;
whole numbers, Python will throw an error.&lt;br /&gt;
&lt;br /&gt;
Example using a named function:&lt;br /&gt;
 def cubed(x):&lt;br /&gt;
     return x**3&lt;br /&gt;
 &lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(cubed, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
The same thing with a lambda:&lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(lambda x: x**3, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
You can also pass Python&#039;s built-in functions straight in:&lt;br /&gt;
 words = [&#039;hello&#039;, &#039;world&#039;, &#039;vpython&#039;]&lt;br /&gt;
 lengths = list(map(len, words))&lt;br /&gt;
 # Result: [5, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Map()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;map(function, list)&amp;lt;/code&amp;gt; runs a function on every single element in a list &lt;br /&gt;
and gives you back all the results. The original list stays the same; you get a &lt;br /&gt;
new list of updated values.&lt;br /&gt;
&lt;br /&gt;
 map(function, list)&lt;br /&gt;
&lt;br /&gt;
So for a list &amp;lt;code&amp;gt;[a, b, c, d]&amp;lt;/code&amp;gt; and some function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;:&lt;br /&gt;
 map(f, [a, b, c, d])  →  [f(a), f(b), f(c), f(d)]&lt;br /&gt;
&lt;br /&gt;
=== Basic example ===&lt;br /&gt;
&lt;br /&gt;
 numlist = [1, 2, 3, 4, 5]&lt;br /&gt;
 result = list(map(lambda x: x * 2, numlist))&lt;br /&gt;
 # Result: [2, 4, 6, 8, 10]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational weight (F = mg) for the masses:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8  # m/s^2&lt;br /&gt;
 masses = [0.5, 1.0, 2.5, 5.0, 10.0]  # kg&lt;br /&gt;
 &lt;br /&gt;
 weights = list(map(lambda m: m * g, masses))&lt;br /&gt;
 # Result: [4.9, 9.8, 24.5, 49.0, 98.0]  Newtons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Kinetic energy of the velocities:&#039;&#039;&#039;&lt;br /&gt;
 mass = 2.0   # kg&lt;br /&gt;
 velocities = [3.0, 5.5, 2.1, 8.0]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 ke_list = list(map(lambda v: 0.5 * mass * v**2, velocities))&lt;br /&gt;
 # Result: [9.0, 30.25, 4.41, 64.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Converting temperature from Celsius to Kelvin:&#039;&#039;&#039;&lt;br /&gt;
 temps_C = [0, 20, 37, 100, -273.15]&lt;br /&gt;
 temps_K = list(map(lambda T: T + 273.15, temps_C))&lt;br /&gt;
 # Result: [273.15, 293.15, 310.15, 373.15, 0.0]  Kelvin&lt;br /&gt;
&lt;br /&gt;
=== map() vs a for-loop ===&lt;br /&gt;
&lt;br /&gt;
Both of these give the same answer, but &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; is more concise:&lt;br /&gt;
&lt;br /&gt;
 # For-loop&lt;br /&gt;
 weights = []&lt;br /&gt;
 for m in masses:&lt;br /&gt;
     weights.append(m * 9.8)&lt;br /&gt;
 &lt;br /&gt;
 # map() — same thing in one line&lt;br /&gt;
 weights = list(map(lambda m: m * 9.8, masses))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Filter()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;filter(function, list)&amp;lt;/code&amp;gt; iterates through a list and only keeps the elements &lt;br /&gt;
where the function returns &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 filter(function, list)&lt;br /&gt;
 filter(lambda x: condition, list)&lt;br /&gt;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
 numbers = [3, 7, 5, 2, 1, 6]&lt;br /&gt;
 result = list(filter(lambda x: x &amp;gt; 3, numbers))&lt;br /&gt;
 # Result: [7, 5, 6]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Retrieving only the fastest particles:&#039;&#039;&#039;&lt;br /&gt;
 speeds = [120, 340, 95, 500, 210, 80]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 fast_particles = list(filter(lambda v: v &amp;gt; 200, speeds))&lt;br /&gt;
 # Result: [340, 500, 210]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Keeping the positive charges:&#039;&#039;&#039;&lt;br /&gt;
 charges = [-1.6e-19, 1.6e-19, -3.2e-19, 3.2e-19, 0, 1.6e-19]  # Coulombs&lt;br /&gt;
 &lt;br /&gt;
 positive = list(filter(lambda q: q &amp;gt; 0, charges))&lt;br /&gt;
 # Result: [1.6e-19, 3.2e-19, 1.6e-19]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Removing particles that exited the simulation boundary:&#039;&#039;&#039;&lt;br /&gt;
 # Each particle p has a .pos.x value for its x position&lt;br /&gt;
 boundary = 10.0  # meters&lt;br /&gt;
 &lt;br /&gt;
 inside = list(filter(lambda p: abs(p.pos.x) &amp;lt; boundary, particles))&lt;br /&gt;
&lt;br /&gt;
=== Passing None as the function ===&lt;br /&gt;
&lt;br /&gt;
If you pass &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; instead of a function, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; removes &lt;br /&gt;
every zero, empty string, &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt; from the list:&lt;br /&gt;
&lt;br /&gt;
 messy = [1, 0, 3, None, 5, 0, 7]&lt;br /&gt;
 clean = list(filter(None, messy))&lt;br /&gt;
 # Result: [1, 3, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;reduce(function, list, starting value)&amp;lt;/code&amp;gt; takes a list and returns a single value. &lt;br /&gt;
&lt;br /&gt;
The function you pass in needs to take two inputs:&lt;br /&gt;
* The running total so far&lt;br /&gt;
* The next item in the list&lt;br /&gt;
&lt;br /&gt;
After each step, the result becomes the new total for the next step.&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 reduce(function, list, starting value)&lt;br /&gt;
&lt;br /&gt;
=== Step-by-step walkthrough ===&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 numbers = [1, 2, 3, 4]&lt;br /&gt;
 result = reduce(lambda x, y: x * y, numbers)&lt;br /&gt;
 # Step 1: x=1, y=2  →  1 * 2 = 2&lt;br /&gt;
 # Step 2: x=2, y=3  →  2 * 3 = 6&lt;br /&gt;
 # Step 3: x=6, y=4  →  6 * 4 = 24&lt;br /&gt;
 # Final result: 24&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Adding up all the masses in a system:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 masses = [1.0, 2.0, 3.0, 4.0]  # kg&lt;br /&gt;
 total_mass = reduce(lambda acc, m: acc + m, masses, 0.0)&lt;br /&gt;
 # Result: 10.0 kg&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Finding the fastest particle in a list:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 speeds = [3.2, 7.8, 1.1, 9.4, 5.5]  # m/s&lt;br /&gt;
 max_speed = reduce(lambda a, b: a if a &amp;gt; b else b, speeds)&lt;br /&gt;
 # Result: 9.4 m/s&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Total work done (W = F·d):&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 forces = [10.0, 25.0, 5.0, 40.0]       # Newtons&lt;br /&gt;
 displacements = [2.0, 1.5, 3.0, 0.5]   # meters&lt;br /&gt;
 &lt;br /&gt;
 work_list = list(map(lambda fd: fd[0] * fd[1], zip(forces, displacements)))&lt;br /&gt;
 total_work = reduce(lambda acc, w: acc + w, work_list, 0.0)&lt;br /&gt;
 # Result: 20.0 + 37.5 + 15.0 + 20.0 = 92.5 Joules&lt;br /&gt;
&lt;br /&gt;
=== Watch out ===&lt;br /&gt;
&lt;br /&gt;
Always give &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; a starting value as the third argument. If the &lt;br /&gt;
list is empty and there is no starting value, Python will crash. With a starting &lt;br /&gt;
value, an empty list will give you that value back instead:&lt;br /&gt;
&lt;br /&gt;
 reduce(lambda acc, x: acc + x, [], 0.0)&lt;br /&gt;
 # Returns 0.0 safely&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Combining map(), filter(), and reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
It is extremely efficient that these functions work well when chained together.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example: kinetic energy&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 # Step 1: filter(): drop all values below 3.0 m/s&lt;br /&gt;
 moving = list(filter(lambda p: p.speed &amp;gt; 3.0, particles))&lt;br /&gt;
 &lt;br /&gt;
 # Step 2: map(): calculate the kinetic energy for each remaining particle&lt;br /&gt;
 ke_list = list(map(lambda p: 0.5 * p.mass * p.speed**2, moving))&lt;br /&gt;
 &lt;br /&gt;
 # Step 3: reduce(): add them all up&lt;br /&gt;
 total_ke = reduce(lambda acc, ke: acc + ke, ke_list, 0.0)&lt;br /&gt;
 print(&amp;quot;Total KE of fast particles:&amp;quot;, round(total_ke, 2), &amp;quot;J&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
This filter → map → reduce pattern shows up very commonly physics simulations. &lt;br /&gt;
Pick a group of objects, apply a formula to each one, then get one final number &lt;br /&gt;
out of it. Below are some examples of these functions chained together.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational potential energy at different heights:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8   # m/s^2&lt;br /&gt;
 m = 2.0   # kg&lt;br /&gt;
 heights = [1.0, 5.0, 10.0, 20.0, 50.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 pe_list = list(map(lambda h: m * g * h, heights))&lt;br /&gt;
 # Result: [19.6, 98.0, 196.0, 392.0, 980.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Electric force on an electron at different distances from a charge (Coulomb&#039;s law):&#039;&#039;&#039;&lt;br /&gt;
 k = 8.99e9   # N·m^2/C^2&lt;br /&gt;
 Q = 1.0e-6   # source charge, Coulombs&lt;br /&gt;
 r_list = [0.1, 0.2, 0.5, 1.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 forces = list(map(lambda r: k * Q * 1.6e-19 / r**2, r_list))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Simulation&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
The following GlowScript simulation shows all three functions working together &lt;br /&gt;
in a real-world physics example:&lt;br /&gt;
[https://trinket.io/glowscript/9e53d57b352f map(), filter(), and reduce() in VPython Physics — Trinket]&lt;br /&gt;
&lt;br /&gt;
The simulation has five spheres in a row, each with a different mass between &lt;br /&gt;
1 and 8 kg and a different speed between 1.5 and 6 m/s. The size of each sphere &lt;br /&gt;
matches its mass.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;map()&#039;&#039;&#039; goes through every mass and calculates the weight using F = mg. &lt;br /&gt;
The weight of each sphere gets printed to the console.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;filter()&#039;&#039;&#039; checks each sphere&#039;s speed and keeps the ones faster &lt;br /&gt;
than 3.0 m/s. &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;reduce()&#039;&#039;&#039; is written by hand since GlowScript does not have the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. It adds up the kinetic energy of every sphere &lt;br /&gt;
one by one until it has one total number for the whole system, which then &lt;br /&gt;
gets printed.&lt;br /&gt;
&lt;br /&gt;
This simulation is an accurate visualization of how all three functions work together.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;References&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
[http://www.bogotobogo.com/python/python_fncs_map_filter_reduce.php 1. Python map, filter, reduce — bogotobogo.com]&lt;br /&gt;
&lt;br /&gt;
[http://book.pythontips.com/en/latest/map_filter.html 2. Map, Filter — Python Tips]&lt;br /&gt;
&lt;br /&gt;
[http://vpython.org/contents/docs/VisualIntro.html 3. VPython Documentation — vpython.org]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functions.html 4. Python 3 Built-in Functions (map, filter) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functools.html 5. functools module (reduce) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/howto/functional.html 6. Functional Programming HOWTO — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-map-function/ 7. Python&#039;s map() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-filter-function/ 8. Python&#039;s filter() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-reduce-function/ 9. Python&#039;s reduce() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-lambda/ 10. Lambda Expressions in Python — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/functional-programming-in-python/ 11. Functional Programming in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/higher-order-functions-in-python/ 12. Higher-Order Functions in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.glowscript.org 13. GlowScript VPython — Official Site]&lt;br /&gt;
&lt;br /&gt;
[https://trinket.io/glowscript 14. GlowScript on Trinket]&lt;br /&gt;
&lt;br /&gt;
[https://vpython.org 15. VPython Official Documentation]&lt;/div&gt;</summary>
		<author><name>Sabrinayang</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48109</id>
		<title>VPython MapReduceFilter</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48109"/>
		<updated>2026-04-27T01:07:56Z</updated>

		<summary type="html">&lt;p&gt;Sabrinayang: /* Background */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;&amp;lt;big&amp;gt;Sabrina Yang - Spring 2026&amp;lt;/big&amp;gt;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Introduction&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; are &lt;br /&gt;
known as higher-order functions. Higher-order functions are functions that &lt;br /&gt;
take other functions as arguments. VPython simulations usually have hundreds of &lt;br /&gt;
objects that all follow the same physics equations, and writing for-loops to do the same &lt;br /&gt;
calculation over and over can be inefficient. &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &lt;br /&gt;
&amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; take care of the for-loops, &lt;br /&gt;
allowing you to focus on the point of the code itself.&lt;br /&gt;
&lt;br /&gt;
Each function does has a different job:&lt;br /&gt;
* &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;: applies a function to every element in a list&lt;br /&gt;
* &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;: iterates through a list and only keeps the elements that meet a certain condition&lt;br /&gt;
* &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;: takes a whole list and turns it into one single value&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Python 3 note:&#039;&#039;&#039; &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; do not execute until you ask for the results. Wrap them in &amp;lt;code&amp;gt;list()&amp;lt;/code&amp;gt; to get &lt;br /&gt;
the results. &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; has to be imported before you can use it:&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; GlowScript does not support lambda expressions or the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. Use regular &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; functions instead of &lt;br /&gt;
lambdas, and write &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; yourself. Here is an example of this &lt;br /&gt;
in the simulation section below.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Background&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, functions are objects, meaning you can:&lt;br /&gt;
* Store a function in a variable&lt;br /&gt;
* Pass a function into another function as an argument&lt;br /&gt;
* Get a function back as a return value&lt;br /&gt;
&lt;br /&gt;
A few reasons why writing code this way is efficient:&lt;br /&gt;
* &#039;&#039;&#039;It is easy to read:&#039;&#039;&#039; You can immediately tell what the program is trying to achieve. There is no complex decoding required.&lt;br /&gt;
* &#039;&#039;&#039;Fewer bugs:&#039;&#039;&#039; Every time you write a for-loop you have to manually keep track of index variables, which is easy to make mistakes. These functions automatically handle the indices, so there is less room for errors.&lt;br /&gt;
* &#039;&#039;&#039;You can chain them:&#039;&#039;&#039; The output of &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; goes straight into &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; without extra steps, which keeps the amount variables needed to a minimum.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Lambda Expressions&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
A &#039;&#039;&#039;lambda expression&#039;&#039;&#039; is a function you write in one line without giving it a name. &lt;br /&gt;
The format is:&lt;br /&gt;
&lt;br /&gt;
 lambda &amp;lt;parameters&amp;gt;: &amp;lt;expression&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function only exists at the point where it is used. These two do the exact same thing:&lt;br /&gt;
&lt;br /&gt;
 # Regular way&lt;br /&gt;
 def square(x):&lt;br /&gt;
     return x**2&lt;br /&gt;
 &lt;br /&gt;
 # Lambda way&lt;br /&gt;
 square = lambda x: x**2&lt;br /&gt;
&lt;br /&gt;
Lambdas save you from having to define a whole separate function when you only &lt;br /&gt;
need it once:&lt;br /&gt;
&lt;br /&gt;
 # Without lambda: needs a function defined somewhere else&lt;br /&gt;
 result = list(map(square, [1, 2, 3, 4]))&lt;br /&gt;
 &lt;br /&gt;
 # With lambda: no separate function needed&lt;br /&gt;
 result = list(map(lambda x: x**2, [1, 2, 3, 4]))&lt;br /&gt;
 # Result: [1, 4, 9, 16]&lt;br /&gt;
&lt;br /&gt;
They can also take two inputs, which comes up when using &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 total = reduce(lambda acc, x: acc + x, [1, 2, 3, 4], 0)&lt;br /&gt;
 # Result: 10&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lambda vs def — when to use which:&#039;&#039;&#039;&lt;br /&gt;
* Use a lambda when the function is short, only needed once, and passed right &lt;br /&gt;
into another function&lt;br /&gt;
* Use &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when the function is longer or used in multiple places&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; Lambdas do not work in GlowScript at all. Always use &lt;br /&gt;
&amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when writing code for Trinket.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Inputs and Type Matching&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
All three functions follow the same basic structure:&lt;br /&gt;
&lt;br /&gt;
 function_name(function, list)&lt;br /&gt;
&lt;br /&gt;
The first argument is the function you want to use. The second is the list of &lt;br /&gt;
data you want to run it on, which can be a list, tuple, etc.&lt;br /&gt;
&lt;br /&gt;
One thing to watch out for: the function has to be able to work with any item &lt;br /&gt;
is in the list. If your list has decimal numbers but your function expects &lt;br /&gt;
whole numbers, Python will throw an error.&lt;br /&gt;
&lt;br /&gt;
Example using a named function:&lt;br /&gt;
 def cubed(x):&lt;br /&gt;
     return x**3&lt;br /&gt;
 &lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(cubed, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
The same thing with a lambda:&lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(lambda x: x**3, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
You can also pass Python&#039;s built-in functions straight in:&lt;br /&gt;
 words = [&#039;hello&#039;, &#039;world&#039;, &#039;vpython&#039;]&lt;br /&gt;
 lengths = list(map(len, words))&lt;br /&gt;
 # Result: [5, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Map()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;map(function, list)&amp;lt;/code&amp;gt; runs a function on every single element in a list &lt;br /&gt;
and gives you back all the results. The original list stays the same; you get a &lt;br /&gt;
new list of updated values.&lt;br /&gt;
&lt;br /&gt;
 map(function, list)&lt;br /&gt;
&lt;br /&gt;
So for a list &amp;lt;code&amp;gt;[a, b, c, d]&amp;lt;/code&amp;gt; and some function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;:&lt;br /&gt;
 map(f, [a, b, c, d])  →  [f(a), f(b), f(c), f(d)]&lt;br /&gt;
&lt;br /&gt;
=== Basic example ===&lt;br /&gt;
&lt;br /&gt;
 numlist = [1, 2, 3, 4, 5]&lt;br /&gt;
 result = list(map(lambda x: x * 2, numlist))&lt;br /&gt;
 # Result: [2, 4, 6, 8, 10]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational weight (F = mg) for the masses:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8  # m/s^2&lt;br /&gt;
 masses = [0.5, 1.0, 2.5, 5.0, 10.0]  # kg&lt;br /&gt;
 &lt;br /&gt;
 weights = list(map(lambda m: m * g, masses))&lt;br /&gt;
 # Result: [4.9, 9.8, 24.5, 49.0, 98.0]  Newtons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Kinetic energy of the velocities:&#039;&#039;&#039;&lt;br /&gt;
 mass = 2.0   # kg&lt;br /&gt;
 velocities = [3.0, 5.5, 2.1, 8.0]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 ke_list = list(map(lambda v: 0.5 * mass * v**2, velocities))&lt;br /&gt;
 # Result: [9.0, 30.25, 4.41, 64.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Converting temperature from Celsius to Kelvin:&#039;&#039;&#039;&lt;br /&gt;
 temps_C = [0, 20, 37, 100, -273.15]&lt;br /&gt;
 temps_K = list(map(lambda T: T + 273.15, temps_C))&lt;br /&gt;
 # Result: [273.15, 293.15, 310.15, 373.15, 0.0]  Kelvin&lt;br /&gt;
&lt;br /&gt;
=== map() vs a for-loop ===&lt;br /&gt;
&lt;br /&gt;
Both of these give the same answer, but &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; is more concise:&lt;br /&gt;
&lt;br /&gt;
 # For-loop&lt;br /&gt;
 weights = []&lt;br /&gt;
 for m in masses:&lt;br /&gt;
     weights.append(m * 9.8)&lt;br /&gt;
 &lt;br /&gt;
 # map() — same thing in one line&lt;br /&gt;
 weights = list(map(lambda m: m * 9.8, masses))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Filter()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;filter(function, list)&amp;lt;/code&amp;gt; iterates through a list and only keeps the elements &lt;br /&gt;
where the function returns &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 filter(function, list)&lt;br /&gt;
 filter(lambda x: condition, list)&lt;br /&gt;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
 numbers = [3, 7, 5, 2, 1, 6]&lt;br /&gt;
 result = list(filter(lambda x: x &amp;gt; 3, numbers))&lt;br /&gt;
 # Result: [7, 5, 6]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Retrieving only the fastest particles:&#039;&#039;&#039;&lt;br /&gt;
 speeds = [120, 340, 95, 500, 210, 80]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 fast_particles = list(filter(lambda v: v &amp;gt; 200, speeds))&lt;br /&gt;
 # Result: [340, 500, 210]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Keeping the positive charges:&#039;&#039;&#039;&lt;br /&gt;
 charges = [-1.6e-19, 1.6e-19, -3.2e-19, 3.2e-19, 0, 1.6e-19]  # Coulombs&lt;br /&gt;
 &lt;br /&gt;
 positive = list(filter(lambda q: q &amp;gt; 0, charges))&lt;br /&gt;
 # Result: [1.6e-19, 3.2e-19, 1.6e-19]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Removing particles that exited the simulation boundary:&#039;&#039;&#039;&lt;br /&gt;
 # Each particle p has a .pos.x value for its x position&lt;br /&gt;
 boundary = 10.0  # meters&lt;br /&gt;
 &lt;br /&gt;
 inside = list(filter(lambda p: abs(p.pos.x) &amp;lt; boundary, particles))&lt;br /&gt;
&lt;br /&gt;
=== Passing None as the function ===&lt;br /&gt;
&lt;br /&gt;
If you pass &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; instead of a function, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; removes &lt;br /&gt;
every zero, empty string, &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt; from the list:&lt;br /&gt;
&lt;br /&gt;
 messy = [1, 0, 3, None, 5, 0, 7]&lt;br /&gt;
 clean = list(filter(None, messy))&lt;br /&gt;
 # Result: [1, 3, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;reduce(function, list, starting value)&amp;lt;/code&amp;gt; takes a list and returns a single value. &lt;br /&gt;
&lt;br /&gt;
The function you pass in needs to take two inputs:&lt;br /&gt;
* The running total so far&lt;br /&gt;
* The next item in the list&lt;br /&gt;
&lt;br /&gt;
After each step, the result becomes the new total for the next step.&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 reduce(function, list, starting value)&lt;br /&gt;
&lt;br /&gt;
=== Step-by-step walkthrough ===&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 numbers = [1, 2, 3, 4]&lt;br /&gt;
 result = reduce(lambda x, y: x * y, numbers)&lt;br /&gt;
 # Step 1: x=1, y=2  →  1 * 2 = 2&lt;br /&gt;
 # Step 2: x=2, y=3  →  2 * 3 = 6&lt;br /&gt;
 # Step 3: x=6, y=4  →  6 * 4 = 24&lt;br /&gt;
 # Final result: 24&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Adding up all the masses in a system:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 masses = [1.0, 2.0, 3.0, 4.0]  # kg&lt;br /&gt;
 total_mass = reduce(lambda acc, m: acc + m, masses, 0.0)&lt;br /&gt;
 # Result: 10.0 kg&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Finding the fastest particle in a list:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 speeds = [3.2, 7.8, 1.1, 9.4, 5.5]  # m/s&lt;br /&gt;
 max_speed = reduce(lambda a, b: a if a &amp;gt; b else b, speeds)&lt;br /&gt;
 # Result: 9.4 m/s&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Total work done (W = F·d):&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 forces = [10.0, 25.0, 5.0, 40.0]       # Newtons&lt;br /&gt;
 displacements = [2.0, 1.5, 3.0, 0.5]   # meters&lt;br /&gt;
 &lt;br /&gt;
 work_list = list(map(lambda fd: fd[0] * fd[1], zip(forces, displacements)))&lt;br /&gt;
 total_work = reduce(lambda acc, w: acc + w, work_list, 0.0)&lt;br /&gt;
 # Result: 20.0 + 37.5 + 15.0 + 20.0 = 92.5 Joules&lt;br /&gt;
&lt;br /&gt;
=== Watch out ===&lt;br /&gt;
&lt;br /&gt;
Always give &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; a starting value as the third argument. If the &lt;br /&gt;
list is empty and there is no starting value, Python will crash. With a starting &lt;br /&gt;
value, an empty list will give you that value back instead:&lt;br /&gt;
&lt;br /&gt;
 reduce(lambda acc, x: acc + x, [], 0.0)&lt;br /&gt;
 # Returns 0.0 safely&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Combining map(), filter(), and reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
It is extremely efficient that these functions work well when chained together.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example: kinetic energy&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 # Step 1: filter(): drop all values below 3.0 m/s&lt;br /&gt;
 moving = list(filter(lambda p: p.speed &amp;gt; 3.0, particles))&lt;br /&gt;
 &lt;br /&gt;
 # Step 2: map(): calculate the kinetic energy for each remaining particle&lt;br /&gt;
 ke_list = list(map(lambda p: 0.5 * p.mass * p.speed**2, moving))&lt;br /&gt;
 &lt;br /&gt;
 # Step 3: reduce(): add them all up&lt;br /&gt;
 total_ke = reduce(lambda acc, ke: acc + ke, ke_list, 0.0)&lt;br /&gt;
 print(&amp;quot;Total KE of fast particles:&amp;quot;, round(total_ke, 2), &amp;quot;J&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
This filter → map → reduce pattern shows up very commonly physics simulations. &lt;br /&gt;
Pick a group of objects, apply a formula to each one, then get one final number &lt;br /&gt;
out of it. Below are some examples of these functions chained together.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational potential energy at different heights:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8   # m/s^2&lt;br /&gt;
 m = 2.0   # kg&lt;br /&gt;
 heights = [1.0, 5.0, 10.0, 20.0, 50.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 pe_list = list(map(lambda h: m * g * h, heights))&lt;br /&gt;
 # Result: [19.6, 98.0, 196.0, 392.0, 980.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Electric force on an electron at different distances from a charge (Coulomb&#039;s law):&#039;&#039;&#039;&lt;br /&gt;
 k = 8.99e9   # N·m^2/C^2&lt;br /&gt;
 Q = 1.0e-6   # source charge, Coulombs&lt;br /&gt;
 r_list = [0.1, 0.2, 0.5, 1.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 forces = list(map(lambda r: k * Q * 1.6e-19 / r**2, r_list))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Simulation&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
The following GlowScript simulation shows all three functions working together &lt;br /&gt;
in a real-world physics example:&lt;br /&gt;
[https://trinket.io/glowscript/9e53d57b352f map(), filter(), and reduce() in VPython Physics — Trinket]&lt;br /&gt;
&lt;br /&gt;
The simulation has five spheres in a row, each with a different mass between &lt;br /&gt;
1 and 8 kg and a different speed between 1.5 and 6 m/s. The size of each sphere &lt;br /&gt;
matches its mass.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;map()&#039;&#039;&#039; goes through every mass and calculates the weight using F = mg. &lt;br /&gt;
The weight of each sphere gets printed to the console.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;filter()&#039;&#039;&#039; checks each sphere&#039;s speed and keeps the ones faster &lt;br /&gt;
than 3.0 m/s. &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;reduce()&#039;&#039;&#039; is written by hand since GlowScript does not have the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. It adds up the kinetic energy of every sphere &lt;br /&gt;
one by one until it has one total number for the whole system, which then &lt;br /&gt;
gets printed.&lt;br /&gt;
&lt;br /&gt;
This simulation is an accurate visualization of how all three functions work together.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;References&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
[http://www.bogotobogo.com/python/python_fncs_map_filter_reduce.php 1. Python map, filter, reduce — bogotobogo.com]&lt;br /&gt;
&lt;br /&gt;
[http://book.pythontips.com/en/latest/map_filter.html 2. Map, Filter — Python Tips]&lt;br /&gt;
&lt;br /&gt;
[http://vpython.org/contents/docs/VisualIntro.html 3. VPython Documentation — vpython.org]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functions.html 4. Python 3 Built-in Functions (map, filter) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functools.html 5. functools module (reduce) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/howto/functional.html 6. Functional Programming HOWTO — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-map-function/ 7. Python&#039;s map() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-filter-function/ 8. Python&#039;s filter() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-reduce-function/ 9. Python&#039;s reduce() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-lambda/ 10. Lambda Expressions in Python — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/functional-programming-in-python/ 11. Functional Programming in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/higher-order-functions-in-python/ 12. Higher-Order Functions in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.glowscript.org 13. GlowScript VPython — Official Site]&lt;br /&gt;
&lt;br /&gt;
[https://trinket.io/glowscript 14. GlowScript on Trinket]&lt;br /&gt;
&lt;br /&gt;
[https://vpython.org 15. VPython Official Documentation]&lt;/div&gt;</summary>
		<author><name>Sabrinayang</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48104</id>
		<title>VPython MapReduceFilter</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48104"/>
		<updated>2026-04-27T01:02:18Z</updated>

		<summary type="html">&lt;p&gt;Sabrinayang: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;&amp;lt;big&amp;gt;Sabrina Yang - Spring 2026&amp;lt;/big&amp;gt;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Introduction&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; are &lt;br /&gt;
known as higher-order functions. Higher-order functions are functions that &lt;br /&gt;
take other functions as arguments. VPython simulations usually have hundreds of &lt;br /&gt;
objects that all follow the same physics equations, and writing for-loops to do the same &lt;br /&gt;
calculation over and over can be inefficient. &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &lt;br /&gt;
&amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; take care of the for-loops, &lt;br /&gt;
allowing you to focus on the point of the code itself.&lt;br /&gt;
&lt;br /&gt;
Each function does has a different job:&lt;br /&gt;
* &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;: applies a function to every element in a list&lt;br /&gt;
* &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;: iterates through a list and only keeps the elements that meet a certain condition&lt;br /&gt;
* &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;: takes a whole list and turns it into one single value&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Python 3 note:&#039;&#039;&#039; &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; do not execute until you ask for the results. Wrap them in &amp;lt;code&amp;gt;list()&amp;lt;/code&amp;gt; to get &lt;br /&gt;
the results. &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; has to be imported before you can use it:&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; GlowScript does not support lambda expressions or the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. Use regular &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; functions instead of &lt;br /&gt;
lambdas, and write &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; yourself. Here is an example of this &lt;br /&gt;
in the simulation section below.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Background&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, functions are objects, meaning you can:&lt;br /&gt;
* Store a function in a variable&lt;br /&gt;
* Pass a function into another function as an argument&lt;br /&gt;
* Get a function back as a return value&lt;br /&gt;
&lt;br /&gt;
A few reasons why writing code this way is efficient:&lt;br /&gt;
* &#039;&#039;&#039;It is easy to read:&#039;&#039;&#039; You can immediately tell what the program &lt;br /&gt;
is doing. It is obvious that the program is saying to &amp;quot;apply this formula to every mass.&amp;quot; &lt;br /&gt;
There is no complex decoding required.&lt;br /&gt;
* &#039;&#039;&#039;Fewer bugs:&#039;&#039;&#039; Every time you write a for-loop you have to manually keep track of index &lt;br /&gt;
variables, which is easy to make mistakes. These functions &lt;br /&gt;
handle the indices, so there is less room for errors.&lt;br /&gt;
* &#039;&#039;&#039;You can chain them:&#039;&#039;&#039; The output of &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; goes straight into &lt;br /&gt;
&amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; without extra steps, which keeps &lt;br /&gt;
the variables clean and short.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Lambda Expressions&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
A &#039;&#039;&#039;lambda expression&#039;&#039;&#039; is a function you write in one line without giving it a name. &lt;br /&gt;
The format is:&lt;br /&gt;
&lt;br /&gt;
 lambda &amp;lt;parameters&amp;gt;: &amp;lt;expression&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function only exists at the point where it is used. These two do the exact same thing:&lt;br /&gt;
&lt;br /&gt;
 # Regular way&lt;br /&gt;
 def square(x):&lt;br /&gt;
     return x**2&lt;br /&gt;
 &lt;br /&gt;
 # Lambda way&lt;br /&gt;
 square = lambda x: x**2&lt;br /&gt;
&lt;br /&gt;
Lambdas save you from having to define a whole separate function when you only &lt;br /&gt;
need it once:&lt;br /&gt;
&lt;br /&gt;
 # Without lambda: needs a function defined somewhere else&lt;br /&gt;
 result = list(map(square, [1, 2, 3, 4]))&lt;br /&gt;
 &lt;br /&gt;
 # With lambda: no separate function needed&lt;br /&gt;
 result = list(map(lambda x: x**2, [1, 2, 3, 4]))&lt;br /&gt;
 # Result: [1, 4, 9, 16]&lt;br /&gt;
&lt;br /&gt;
They can also take two inputs, which comes up when using &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 total = reduce(lambda acc, x: acc + x, [1, 2, 3, 4], 0)&lt;br /&gt;
 # Result: 10&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lambda vs def — when to use which:&#039;&#039;&#039;&lt;br /&gt;
* Use a lambda when the function is short, only needed once, and passed right &lt;br /&gt;
into another function&lt;br /&gt;
* Use &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when the function is longer or used in multiple places&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; Lambdas do not work in GlowScript at all. Always use &lt;br /&gt;
&amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when writing code for Trinket.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Inputs and Type Matching&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
All three functions follow the same basic structure:&lt;br /&gt;
&lt;br /&gt;
 function_name(function, list)&lt;br /&gt;
&lt;br /&gt;
The first argument is the function you want to use. The second is the list of &lt;br /&gt;
data you want to run it on, which can be a list, tuple, etc.&lt;br /&gt;
&lt;br /&gt;
One thing to watch out for: the function has to be able to work with any item &lt;br /&gt;
is in the list. If your list has decimal numbers but your function expects &lt;br /&gt;
whole numbers, Python will throw an error.&lt;br /&gt;
&lt;br /&gt;
Example using a named function:&lt;br /&gt;
 def cubed(x):&lt;br /&gt;
     return x**3&lt;br /&gt;
 &lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(cubed, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
The same thing with a lambda:&lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(lambda x: x**3, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
You can also pass Python&#039;s built-in functions straight in:&lt;br /&gt;
 words = [&#039;hello&#039;, &#039;world&#039;, &#039;vpython&#039;]&lt;br /&gt;
 lengths = list(map(len, words))&lt;br /&gt;
 # Result: [5, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Map()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;map(function, list)&amp;lt;/code&amp;gt; runs a function on every single element in a list &lt;br /&gt;
and gives you back all the results. The original list stays the same; you get a &lt;br /&gt;
new list of updated values.&lt;br /&gt;
&lt;br /&gt;
 map(function, list)&lt;br /&gt;
&lt;br /&gt;
So for a list &amp;lt;code&amp;gt;[a, b, c, d]&amp;lt;/code&amp;gt; and some function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;:&lt;br /&gt;
 map(f, [a, b, c, d])  →  [f(a), f(b), f(c), f(d)]&lt;br /&gt;
&lt;br /&gt;
=== Basic example ===&lt;br /&gt;
&lt;br /&gt;
 numlist = [1, 2, 3, 4, 5]&lt;br /&gt;
 result = list(map(lambda x: x * 2, numlist))&lt;br /&gt;
 # Result: [2, 4, 6, 8, 10]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational weight (F = mg) for the masses:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8  # m/s^2&lt;br /&gt;
 masses = [0.5, 1.0, 2.5, 5.0, 10.0]  # kg&lt;br /&gt;
 &lt;br /&gt;
 weights = list(map(lambda m: m * g, masses))&lt;br /&gt;
 # Result: [4.9, 9.8, 24.5, 49.0, 98.0]  Newtons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Kinetic energy of the velocities:&#039;&#039;&#039;&lt;br /&gt;
 mass = 2.0   # kg&lt;br /&gt;
 velocities = [3.0, 5.5, 2.1, 8.0]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 ke_list = list(map(lambda v: 0.5 * mass * v**2, velocities))&lt;br /&gt;
 # Result: [9.0, 30.25, 4.41, 64.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Converting temperature from Celsius to Kelvin:&#039;&#039;&#039;&lt;br /&gt;
 temps_C = [0, 20, 37, 100, -273.15]&lt;br /&gt;
 temps_K = list(map(lambda T: T + 273.15, temps_C))&lt;br /&gt;
 # Result: [273.15, 293.15, 310.15, 373.15, 0.0]  Kelvin&lt;br /&gt;
&lt;br /&gt;
=== map() vs a for-loop ===&lt;br /&gt;
&lt;br /&gt;
Both of these give the same answer, but &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; is more concise:&lt;br /&gt;
&lt;br /&gt;
 # For-loop&lt;br /&gt;
 weights = []&lt;br /&gt;
 for m in masses:&lt;br /&gt;
     weights.append(m * 9.8)&lt;br /&gt;
 &lt;br /&gt;
 # map() — same thing in one line&lt;br /&gt;
 weights = list(map(lambda m: m * 9.8, masses))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Filter()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;filter(function, list)&amp;lt;/code&amp;gt; iterates through a list and only keeps the elements &lt;br /&gt;
where the function returns &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 filter(function, list)&lt;br /&gt;
 filter(lambda x: condition, list)&lt;br /&gt;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
 numbers = [3, 7, 5, 2, 1, 6]&lt;br /&gt;
 result = list(filter(lambda x: x &amp;gt; 3, numbers))&lt;br /&gt;
 # Result: [7, 5, 6]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Retrieving only the fastest particles:&#039;&#039;&#039;&lt;br /&gt;
 speeds = [120, 340, 95, 500, 210, 80]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 fast_particles = list(filter(lambda v: v &amp;gt; 200, speeds))&lt;br /&gt;
 # Result: [340, 500, 210]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Keeping the positive charges:&#039;&#039;&#039;&lt;br /&gt;
 charges = [-1.6e-19, 1.6e-19, -3.2e-19, 3.2e-19, 0, 1.6e-19]  # Coulombs&lt;br /&gt;
 &lt;br /&gt;
 positive = list(filter(lambda q: q &amp;gt; 0, charges))&lt;br /&gt;
 # Result: [1.6e-19, 3.2e-19, 1.6e-19]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Removing particles that exited the simulation boundary:&#039;&#039;&#039;&lt;br /&gt;
 # Each particle p has a .pos.x value for its x position&lt;br /&gt;
 boundary = 10.0  # meters&lt;br /&gt;
 &lt;br /&gt;
 inside = list(filter(lambda p: abs(p.pos.x) &amp;lt; boundary, particles))&lt;br /&gt;
&lt;br /&gt;
=== Passing None as the function ===&lt;br /&gt;
&lt;br /&gt;
If you pass &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; instead of a function, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; removes &lt;br /&gt;
every zero, empty string, &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt; from the list:&lt;br /&gt;
&lt;br /&gt;
 messy = [1, 0, 3, None, 5, 0, 7]&lt;br /&gt;
 clean = list(filter(None, messy))&lt;br /&gt;
 # Result: [1, 3, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;reduce(function, list, starting value)&amp;lt;/code&amp;gt; takes a list and returns a single value. &lt;br /&gt;
&lt;br /&gt;
The function you pass in needs to take two inputs:&lt;br /&gt;
* The running total so far&lt;br /&gt;
* The next item in the list&lt;br /&gt;
&lt;br /&gt;
After each step, the result becomes the new total for the next step.&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 reduce(function, list, starting value)&lt;br /&gt;
&lt;br /&gt;
=== Step-by-step walkthrough ===&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 numbers = [1, 2, 3, 4]&lt;br /&gt;
 result = reduce(lambda x, y: x * y, numbers)&lt;br /&gt;
 # Step 1: x=1, y=2  →  1 * 2 = 2&lt;br /&gt;
 # Step 2: x=2, y=3  →  2 * 3 = 6&lt;br /&gt;
 # Step 3: x=6, y=4  →  6 * 4 = 24&lt;br /&gt;
 # Final result: 24&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Adding up all the masses in a system:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 masses = [1.0, 2.0, 3.0, 4.0]  # kg&lt;br /&gt;
 total_mass = reduce(lambda acc, m: acc + m, masses, 0.0)&lt;br /&gt;
 # Result: 10.0 kg&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Finding the fastest particle in a list:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 speeds = [3.2, 7.8, 1.1, 9.4, 5.5]  # m/s&lt;br /&gt;
 max_speed = reduce(lambda a, b: a if a &amp;gt; b else b, speeds)&lt;br /&gt;
 # Result: 9.4 m/s&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Total work done (W = F·d):&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 forces = [10.0, 25.0, 5.0, 40.0]       # Newtons&lt;br /&gt;
 displacements = [2.0, 1.5, 3.0, 0.5]   # meters&lt;br /&gt;
 &lt;br /&gt;
 work_list = list(map(lambda fd: fd[0] * fd[1], zip(forces, displacements)))&lt;br /&gt;
 total_work = reduce(lambda acc, w: acc + w, work_list, 0.0)&lt;br /&gt;
 # Result: 20.0 + 37.5 + 15.0 + 20.0 = 92.5 Joules&lt;br /&gt;
&lt;br /&gt;
=== Watch out ===&lt;br /&gt;
&lt;br /&gt;
Always give &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; a starting value as the third argument. If the &lt;br /&gt;
list is empty and there is no starting value, Python will crash. With a starting &lt;br /&gt;
value, an empty list will give you that value back instead:&lt;br /&gt;
&lt;br /&gt;
 reduce(lambda acc, x: acc + x, [], 0.0)&lt;br /&gt;
 # Returns 0.0 safely&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Combining map(), filter(), and reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
It is extremely efficient that these functions work well when chained together.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example: kinetic energy&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 # Step 1: filter(): drop all values below 3.0 m/s&lt;br /&gt;
 moving = list(filter(lambda p: p.speed &amp;gt; 3.0, particles))&lt;br /&gt;
 &lt;br /&gt;
 # Step 2: map(): calculate the kinetic energy for each remaining particle&lt;br /&gt;
 ke_list = list(map(lambda p: 0.5 * p.mass * p.speed**2, moving))&lt;br /&gt;
 &lt;br /&gt;
 # Step 3: reduce(): add them all up&lt;br /&gt;
 total_ke = reduce(lambda acc, ke: acc + ke, ke_list, 0.0)&lt;br /&gt;
 print(&amp;quot;Total KE of fast particles:&amp;quot;, round(total_ke, 2), &amp;quot;J&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
This filter → map → reduce pattern shows up very commonly physics simulations. &lt;br /&gt;
Pick a group of objects, apply a formula to each one, then get one final number &lt;br /&gt;
out of it. Below are some examples of these functions chained together.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational potential energy at different heights:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8   # m/s^2&lt;br /&gt;
 m = 2.0   # kg&lt;br /&gt;
 heights = [1.0, 5.0, 10.0, 20.0, 50.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 pe_list = list(map(lambda h: m * g * h, heights))&lt;br /&gt;
 # Result: [19.6, 98.0, 196.0, 392.0, 980.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Electric force on an electron at different distances from a charge (Coulomb&#039;s law):&#039;&#039;&#039;&lt;br /&gt;
 k = 8.99e9   # N·m^2/C^2&lt;br /&gt;
 Q = 1.0e-6   # source charge, Coulombs&lt;br /&gt;
 r_list = [0.1, 0.2, 0.5, 1.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 forces = list(map(lambda r: k * Q * 1.6e-19 / r**2, r_list))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Simulation&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
The following GlowScript simulation shows all three functions working together &lt;br /&gt;
in a real-world physics example:&lt;br /&gt;
[https://trinket.io/glowscript/9e53d57b352f map(), filter(), and reduce() in VPython Physics — Trinket]&lt;br /&gt;
&lt;br /&gt;
The simulation has five spheres in a row, each with a different mass between &lt;br /&gt;
1 and 8 kg and a different speed between 1.5 and 6 m/s. The size of each sphere &lt;br /&gt;
matches its mass.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;map()&#039;&#039;&#039; goes through every mass and calculates the weight using F = mg. &lt;br /&gt;
The weight of each sphere gets printed to the console.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;filter()&#039;&#039;&#039; checks each sphere&#039;s speed and keeps the ones faster &lt;br /&gt;
than 3.0 m/s. &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;reduce()&#039;&#039;&#039; is written by hand since GlowScript does not have the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. It adds up the kinetic energy of every sphere &lt;br /&gt;
one by one until it has one total number for the whole system, which then &lt;br /&gt;
gets printed.&lt;br /&gt;
&lt;br /&gt;
This simulation is an accurate visualization of how all three functions work together.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;References&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
[http://www.bogotobogo.com/python/python_fncs_map_filter_reduce.php 1. Python map, filter, reduce — bogotobogo.com]&lt;br /&gt;
&lt;br /&gt;
[http://book.pythontips.com/en/latest/map_filter.html 2. Map, Filter — Python Tips]&lt;br /&gt;
&lt;br /&gt;
[http://vpython.org/contents/docs/VisualIntro.html 3. VPython Documentation — vpython.org]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functions.html 4. Python 3 Built-in Functions (map, filter) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functools.html 5. functools module (reduce) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/howto/functional.html 6. Functional Programming HOWTO — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-map-function/ 7. Python&#039;s map() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-filter-function/ 8. Python&#039;s filter() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-reduce-function/ 9. Python&#039;s reduce() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-lambda/ 10. Lambda Expressions in Python — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/functional-programming-in-python/ 11. Functional Programming in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/higher-order-functions-in-python/ 12. Higher-Order Functions in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.glowscript.org 13. GlowScript VPython — Official Site]&lt;br /&gt;
&lt;br /&gt;
[https://trinket.io/glowscript 14. GlowScript on Trinket]&lt;br /&gt;
&lt;br /&gt;
[https://vpython.org 15. VPython Official Documentation]&lt;/div&gt;</summary>
		<author><name>Sabrinayang</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48102</id>
		<title>VPython MapReduceFilter</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48102"/>
		<updated>2026-04-27T01:01:30Z</updated>

		<summary type="html">&lt;p&gt;Sabrinayang: /* Interactive Simulation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;&amp;lt;big&amp;gt;Sabrina Yang - Spring 2026&amp;lt;/big&amp;gt;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Introduction&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; are &lt;br /&gt;
known as higher-order functions. Higher-order functions are functions that &lt;br /&gt;
take other functions as arguments. VPython simulations usually have hundreds of &lt;br /&gt;
objects that all follow the same physics equations, and writing for-loops to do the same &lt;br /&gt;
calculation over and over can be inefficient. &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &lt;br /&gt;
&amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; take care of the for-loops, &lt;br /&gt;
allowing you to focus on the point of the code itself.&lt;br /&gt;
&lt;br /&gt;
Each function does something different:&lt;br /&gt;
* &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;: applies a function to every element in a list&lt;br /&gt;
* &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;: iterates through a list and only keeps the elements that meet a certain condition&lt;br /&gt;
* &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;: takes a whole list and turns it into one single value&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Python 3 note:&#039;&#039;&#039; &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; do not execute until you ask for the results. Wrap them in &amp;lt;code&amp;gt;list()&amp;lt;/code&amp;gt; to get &lt;br /&gt;
the results. &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; has to be imported before you can use it:&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; GlowScript does not support lambda expressions or the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. Use regular &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; functions instead of &lt;br /&gt;
lambdas, and write &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; yourself. Here is an example of this &lt;br /&gt;
in the simulation section below.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Background&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, functions are objects, meaning you can:&lt;br /&gt;
* Store a function in a variable&lt;br /&gt;
* Pass a function into another function as an argument&lt;br /&gt;
* Get a function back as a return value&lt;br /&gt;
&lt;br /&gt;
A few reasons why writing code this way is efficient:&lt;br /&gt;
* &#039;&#039;&#039;It is easy to read:&#039;&#039;&#039; You can immediately tell what the program &lt;br /&gt;
is doing. It is obvious that the program is saying to &amp;quot;apply this formula to every mass.&amp;quot; &lt;br /&gt;
There is no complex decoding required.&lt;br /&gt;
* &#039;&#039;&#039;Fewer bugs:&#039;&#039;&#039; Every time you write a for-loop you have to manually keep track of index &lt;br /&gt;
variables, which is easy to make mistakes. These functions &lt;br /&gt;
handle the indices, so there is less room for errors.&lt;br /&gt;
* &#039;&#039;&#039;You can chain them:&#039;&#039;&#039; The output of &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; goes straight into &lt;br /&gt;
&amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; without extra steps, which keeps &lt;br /&gt;
the variables clean and short.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Lambda Expressions&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
A &#039;&#039;&#039;lambda expression&#039;&#039;&#039; is a function you write in one line without giving it a name. &lt;br /&gt;
The format is:&lt;br /&gt;
&lt;br /&gt;
 lambda &amp;lt;parameters&amp;gt;: &amp;lt;expression&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function only exists at the point where it is used. These two do the exact same thing:&lt;br /&gt;
&lt;br /&gt;
 # Regular way&lt;br /&gt;
 def square(x):&lt;br /&gt;
     return x**2&lt;br /&gt;
 &lt;br /&gt;
 # Lambda way&lt;br /&gt;
 square = lambda x: x**2&lt;br /&gt;
&lt;br /&gt;
Lambdas save you from having to define a whole separate function when you only &lt;br /&gt;
need it once:&lt;br /&gt;
&lt;br /&gt;
 # Without lambda: needs a function defined somewhere else&lt;br /&gt;
 result = list(map(square, [1, 2, 3, 4]))&lt;br /&gt;
 &lt;br /&gt;
 # With lambda: no separate function needed&lt;br /&gt;
 result = list(map(lambda x: x**2, [1, 2, 3, 4]))&lt;br /&gt;
 # Result: [1, 4, 9, 16]&lt;br /&gt;
&lt;br /&gt;
They can also take two inputs, which comes up when using &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 total = reduce(lambda acc, x: acc + x, [1, 2, 3, 4], 0)&lt;br /&gt;
 # Result: 10&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lambda vs def — when to use which:&#039;&#039;&#039;&lt;br /&gt;
* Use a lambda when the function is short, only needed once, and passed right &lt;br /&gt;
into another function&lt;br /&gt;
* Use &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when the function is longer or used in multiple places&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; Lambdas do not work in GlowScript at all. Always use &lt;br /&gt;
&amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when writing code for Trinket.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Inputs and Type Matching&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
All three functions follow the same basic structure:&lt;br /&gt;
&lt;br /&gt;
 function_name(function, list)&lt;br /&gt;
&lt;br /&gt;
The first argument is the function you want to use. The second is the list of &lt;br /&gt;
data you want to run it on, which can be a list, tuple, etc.&lt;br /&gt;
&lt;br /&gt;
One thing to watch out for: the function has to be able to work with any item &lt;br /&gt;
is in the list. If your list has decimal numbers but your function expects &lt;br /&gt;
whole numbers, Python will throw an error.&lt;br /&gt;
&lt;br /&gt;
Example using a named function:&lt;br /&gt;
 def cubed(x):&lt;br /&gt;
     return x**3&lt;br /&gt;
 &lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(cubed, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
The same thing with a lambda:&lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(lambda x: x**3, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
You can also pass Python&#039;s built-in functions straight in:&lt;br /&gt;
 words = [&#039;hello&#039;, &#039;world&#039;, &#039;vpython&#039;]&lt;br /&gt;
 lengths = list(map(len, words))&lt;br /&gt;
 # Result: [5, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Map()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;map(function, list)&amp;lt;/code&amp;gt; runs a function on every single element in a list &lt;br /&gt;
and gives you back all the results. The original list stays the same; you get a &lt;br /&gt;
new list of updated values.&lt;br /&gt;
&lt;br /&gt;
 map(function, list)&lt;br /&gt;
&lt;br /&gt;
So for a list &amp;lt;code&amp;gt;[a, b, c, d]&amp;lt;/code&amp;gt; and some function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;:&lt;br /&gt;
 map(f, [a, b, c, d])  →  [f(a), f(b), f(c), f(d)]&lt;br /&gt;
&lt;br /&gt;
=== Basic example ===&lt;br /&gt;
&lt;br /&gt;
 numlist = [1, 2, 3, 4, 5]&lt;br /&gt;
 result = list(map(lambda x: x * 2, numlist))&lt;br /&gt;
 # Result: [2, 4, 6, 8, 10]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational weight (F = mg) for the masses:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8  # m/s^2&lt;br /&gt;
 masses = [0.5, 1.0, 2.5, 5.0, 10.0]  # kg&lt;br /&gt;
 &lt;br /&gt;
 weights = list(map(lambda m: m * g, masses))&lt;br /&gt;
 # Result: [4.9, 9.8, 24.5, 49.0, 98.0]  Newtons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Kinetic energy of the velocities:&#039;&#039;&#039;&lt;br /&gt;
 mass = 2.0   # kg&lt;br /&gt;
 velocities = [3.0, 5.5, 2.1, 8.0]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 ke_list = list(map(lambda v: 0.5 * mass * v**2, velocities))&lt;br /&gt;
 # Result: [9.0, 30.25, 4.41, 64.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Converting temperature from Celsius to Kelvin:&#039;&#039;&#039;&lt;br /&gt;
 temps_C = [0, 20, 37, 100, -273.15]&lt;br /&gt;
 temps_K = list(map(lambda T: T + 273.15, temps_C))&lt;br /&gt;
 # Result: [273.15, 293.15, 310.15, 373.15, 0.0]  Kelvin&lt;br /&gt;
&lt;br /&gt;
=== map() vs a for-loop ===&lt;br /&gt;
&lt;br /&gt;
Both of these give the same answer, but &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; is more concise:&lt;br /&gt;
&lt;br /&gt;
 # For-loop&lt;br /&gt;
 weights = []&lt;br /&gt;
 for m in masses:&lt;br /&gt;
     weights.append(m * 9.8)&lt;br /&gt;
 &lt;br /&gt;
 # map() — same thing in one line&lt;br /&gt;
 weights = list(map(lambda m: m * 9.8, masses))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Filter()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;filter(function, list)&amp;lt;/code&amp;gt; iterates through a list and only keeps the elements &lt;br /&gt;
where the function returns &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 filter(function, list)&lt;br /&gt;
 filter(lambda x: condition, list)&lt;br /&gt;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
 numbers = [3, 7, 5, 2, 1, 6]&lt;br /&gt;
 result = list(filter(lambda x: x &amp;gt; 3, numbers))&lt;br /&gt;
 # Result: [7, 5, 6]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Retrieving only the fastest particles:&#039;&#039;&#039;&lt;br /&gt;
 speeds = [120, 340, 95, 500, 210, 80]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 fast_particles = list(filter(lambda v: v &amp;gt; 200, speeds))&lt;br /&gt;
 # Result: [340, 500, 210]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Keeping the positive charges:&#039;&#039;&#039;&lt;br /&gt;
 charges = [-1.6e-19, 1.6e-19, -3.2e-19, 3.2e-19, 0, 1.6e-19]  # Coulombs&lt;br /&gt;
 &lt;br /&gt;
 positive = list(filter(lambda q: q &amp;gt; 0, charges))&lt;br /&gt;
 # Result: [1.6e-19, 3.2e-19, 1.6e-19]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Removing particles that exited the simulation boundary:&#039;&#039;&#039;&lt;br /&gt;
 # Each particle p has a .pos.x value for its x position&lt;br /&gt;
 boundary = 10.0  # meters&lt;br /&gt;
 &lt;br /&gt;
 inside = list(filter(lambda p: abs(p.pos.x) &amp;lt; boundary, particles))&lt;br /&gt;
&lt;br /&gt;
=== Passing None as the function ===&lt;br /&gt;
&lt;br /&gt;
If you pass &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; instead of a function, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; removes &lt;br /&gt;
every zero, empty string, &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt; from the list:&lt;br /&gt;
&lt;br /&gt;
 messy = [1, 0, 3, None, 5, 0, 7]&lt;br /&gt;
 clean = list(filter(None, messy))&lt;br /&gt;
 # Result: [1, 3, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;reduce(function, list, starting value)&amp;lt;/code&amp;gt; takes a list and returns a single value. &lt;br /&gt;
&lt;br /&gt;
The function you pass in needs to take two inputs:&lt;br /&gt;
* The running total so far&lt;br /&gt;
* The next item in the list&lt;br /&gt;
&lt;br /&gt;
After each step, the result becomes the new total for the next step.&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 reduce(function, list, starting value)&lt;br /&gt;
&lt;br /&gt;
=== Step-by-step walkthrough ===&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 numbers = [1, 2, 3, 4]&lt;br /&gt;
 result = reduce(lambda x, y: x * y, numbers)&lt;br /&gt;
 # Step 1: x=1, y=2  →  1 * 2 = 2&lt;br /&gt;
 # Step 2: x=2, y=3  →  2 * 3 = 6&lt;br /&gt;
 # Step 3: x=6, y=4  →  6 * 4 = 24&lt;br /&gt;
 # Final result: 24&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Adding up all the masses in a system:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 masses = [1.0, 2.0, 3.0, 4.0]  # kg&lt;br /&gt;
 total_mass = reduce(lambda acc, m: acc + m, masses, 0.0)&lt;br /&gt;
 # Result: 10.0 kg&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Finding the fastest particle in a list:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 speeds = [3.2, 7.8, 1.1, 9.4, 5.5]  # m/s&lt;br /&gt;
 max_speed = reduce(lambda a, b: a if a &amp;gt; b else b, speeds)&lt;br /&gt;
 # Result: 9.4 m/s&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Total work done (W = F·d):&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 forces = [10.0, 25.0, 5.0, 40.0]       # Newtons&lt;br /&gt;
 displacements = [2.0, 1.5, 3.0, 0.5]   # meters&lt;br /&gt;
 &lt;br /&gt;
 work_list = list(map(lambda fd: fd[0] * fd[1], zip(forces, displacements)))&lt;br /&gt;
 total_work = reduce(lambda acc, w: acc + w, work_list, 0.0)&lt;br /&gt;
 # Result: 20.0 + 37.5 + 15.0 + 20.0 = 92.5 Joules&lt;br /&gt;
&lt;br /&gt;
=== Watch out ===&lt;br /&gt;
&lt;br /&gt;
Always give &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; a starting value as the third argument. If the &lt;br /&gt;
list is empty and there is no starting value, Python will crash. With a starting &lt;br /&gt;
value, an empty list will give you that value back instead:&lt;br /&gt;
&lt;br /&gt;
 reduce(lambda acc, x: acc + x, [], 0.0)&lt;br /&gt;
 # Returns 0.0 safely&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Combining map(), filter(), and reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
It is extremely efficient that these functions work well when chained together.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example: kinetic energy&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 # Step 1: filter(): drop all values below 3.0 m/s&lt;br /&gt;
 moving = list(filter(lambda p: p.speed &amp;gt; 3.0, particles))&lt;br /&gt;
 &lt;br /&gt;
 # Step 2: map(): calculate the kinetic energy for each remaining particle&lt;br /&gt;
 ke_list = list(map(lambda p: 0.5 * p.mass * p.speed**2, moving))&lt;br /&gt;
 &lt;br /&gt;
 # Step 3: reduce(): add them all up&lt;br /&gt;
 total_ke = reduce(lambda acc, ke: acc + ke, ke_list, 0.0)&lt;br /&gt;
 print(&amp;quot;Total KE of fast particles:&amp;quot;, round(total_ke, 2), &amp;quot;J&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
This filter → map → reduce pattern shows up very commonly physics simulations. &lt;br /&gt;
Pick a group of objects, apply a formula to each one, then get one final number &lt;br /&gt;
out of it. Below are some examples of these functions chained together.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational potential energy at different heights:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8   # m/s^2&lt;br /&gt;
 m = 2.0   # kg&lt;br /&gt;
 heights = [1.0, 5.0, 10.0, 20.0, 50.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 pe_list = list(map(lambda h: m * g * h, heights))&lt;br /&gt;
 # Result: [19.6, 98.0, 196.0, 392.0, 980.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Electric force on an electron at different distances from a charge (Coulomb&#039;s law):&#039;&#039;&#039;&lt;br /&gt;
 k = 8.99e9   # N·m^2/C^2&lt;br /&gt;
 Q = 1.0e-6   # source charge, Coulombs&lt;br /&gt;
 r_list = [0.1, 0.2, 0.5, 1.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 forces = list(map(lambda r: k * Q * 1.6e-19 / r**2, r_list))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Simulation&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
The following GlowScript simulation shows all three functions working together &lt;br /&gt;
in a real-world physics example:&lt;br /&gt;
[https://trinket.io/glowscript/9e53d57b352f map(), filter(), and reduce() in VPython Physics — Trinket]&lt;br /&gt;
&lt;br /&gt;
The simulation has five spheres in a row, each with a different mass between &lt;br /&gt;
1 and 8 kg and a different speed between 1.5 and 6 m/s. The size of each sphere &lt;br /&gt;
matches its mass.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;map()&#039;&#039;&#039; goes through every mass and calculates the weight using F = mg. &lt;br /&gt;
The weight of each sphere gets printed to the console.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;filter()&#039;&#039;&#039; checks each sphere&#039;s speed and keeps the ones faster &lt;br /&gt;
than 3.0 m/s. &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;reduce()&#039;&#039;&#039; is written by hand since GlowScript does not have the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. It adds up the kinetic energy of every sphere &lt;br /&gt;
one by one until it has one total number for the whole system, which then &lt;br /&gt;
gets printed.&lt;br /&gt;
&lt;br /&gt;
This simulation is an accurate visualization of how all three functions work together.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;References&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
[http://www.bogotobogo.com/python/python_fncs_map_filter_reduce.php 1. Python map, filter, reduce — bogotobogo.com]&lt;br /&gt;
&lt;br /&gt;
[http://book.pythontips.com/en/latest/map_filter.html 2. Map, Filter — Python Tips]&lt;br /&gt;
&lt;br /&gt;
[http://vpython.org/contents/docs/VisualIntro.html 3. VPython Documentation — vpython.org]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functions.html 4. Python 3 Built-in Functions (map, filter) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functools.html 5. functools module (reduce) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/howto/functional.html 6. Functional Programming HOWTO — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-map-function/ 7. Python&#039;s map() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-filter-function/ 8. Python&#039;s filter() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-reduce-function/ 9. Python&#039;s reduce() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-lambda/ 10. Lambda Expressions in Python — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/functional-programming-in-python/ 11. Functional Programming in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/higher-order-functions-in-python/ 12. Higher-Order Functions in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.glowscript.org 13. GlowScript VPython — Official Site]&lt;br /&gt;
&lt;br /&gt;
[https://trinket.io/glowscript 14. GlowScript on Trinket]&lt;br /&gt;
&lt;br /&gt;
[https://vpython.org 15. VPython Official Documentation]&lt;/div&gt;</summary>
		<author><name>Sabrinayang</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48100</id>
		<title>VPython MapReduceFilter</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48100"/>
		<updated>2026-04-27T01:00:20Z</updated>

		<summary type="html">&lt;p&gt;Sabrinayang: /* Combining map(), filter(), and reduce() */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;&amp;lt;big&amp;gt;Sabrina Yang - Spring 2026&amp;lt;/big&amp;gt;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Introduction&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; are &lt;br /&gt;
known as higher-order functions. Higher-order functions are functions that &lt;br /&gt;
take other functions as arguments. VPython simulations usually have hundreds of &lt;br /&gt;
objects that all follow the same physics equations, and writing for-loops to do the same &lt;br /&gt;
calculation over and over can be inefficient. &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &lt;br /&gt;
&amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; take care of the for-loops, &lt;br /&gt;
allowing you to focus on the point of the code itself.&lt;br /&gt;
&lt;br /&gt;
Each function does something different:&lt;br /&gt;
* &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;: applies a function to every element in a list&lt;br /&gt;
* &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;: iterates through a list and only keeps the elements that meet a certain condition&lt;br /&gt;
* &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;: takes a whole list and turns it into one single value&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Python 3 note:&#039;&#039;&#039; &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; do not execute until you ask for the results. Wrap them in &amp;lt;code&amp;gt;list()&amp;lt;/code&amp;gt; to get &lt;br /&gt;
the results. &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; has to be imported before you can use it:&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; GlowScript does not support lambda expressions or the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. Use regular &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; functions instead of &lt;br /&gt;
lambdas, and write &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; yourself. Here is an example of this &lt;br /&gt;
in the simulation section below.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Background&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, functions are objects, meaning you can:&lt;br /&gt;
* Store a function in a variable&lt;br /&gt;
* Pass a function into another function as an argument&lt;br /&gt;
* Get a function back as a return value&lt;br /&gt;
&lt;br /&gt;
A few reasons why writing code this way is efficient:&lt;br /&gt;
* &#039;&#039;&#039;It is easy to read:&#039;&#039;&#039; You can immediately tell what the program &lt;br /&gt;
is doing. It is obvious that the program is saying to &amp;quot;apply this formula to every mass.&amp;quot; &lt;br /&gt;
There is no complex decoding required.&lt;br /&gt;
* &#039;&#039;&#039;Fewer bugs:&#039;&#039;&#039; Every time you write a for-loop you have to manually keep track of index &lt;br /&gt;
variables, which is easy to make mistakes. These functions &lt;br /&gt;
handle the indices, so there is less room for errors.&lt;br /&gt;
* &#039;&#039;&#039;You can chain them:&#039;&#039;&#039; The output of &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; goes straight into &lt;br /&gt;
&amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; without extra steps, which keeps &lt;br /&gt;
the variables clean and short.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Lambda Expressions&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
A &#039;&#039;&#039;lambda expression&#039;&#039;&#039; is a function you write in one line without giving it a name. &lt;br /&gt;
The format is:&lt;br /&gt;
&lt;br /&gt;
 lambda &amp;lt;parameters&amp;gt;: &amp;lt;expression&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function only exists at the point where it is used. These two do the exact same thing:&lt;br /&gt;
&lt;br /&gt;
 # Regular way&lt;br /&gt;
 def square(x):&lt;br /&gt;
     return x**2&lt;br /&gt;
 &lt;br /&gt;
 # Lambda way&lt;br /&gt;
 square = lambda x: x**2&lt;br /&gt;
&lt;br /&gt;
Lambdas save you from having to define a whole separate function when you only &lt;br /&gt;
need it once:&lt;br /&gt;
&lt;br /&gt;
 # Without lambda: needs a function defined somewhere else&lt;br /&gt;
 result = list(map(square, [1, 2, 3, 4]))&lt;br /&gt;
 &lt;br /&gt;
 # With lambda: no separate function needed&lt;br /&gt;
 result = list(map(lambda x: x**2, [1, 2, 3, 4]))&lt;br /&gt;
 # Result: [1, 4, 9, 16]&lt;br /&gt;
&lt;br /&gt;
They can also take two inputs, which comes up when using &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 total = reduce(lambda acc, x: acc + x, [1, 2, 3, 4], 0)&lt;br /&gt;
 # Result: 10&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lambda vs def — when to use which:&#039;&#039;&#039;&lt;br /&gt;
* Use a lambda when the function is short, only needed once, and passed right &lt;br /&gt;
into another function&lt;br /&gt;
* Use &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when the function is longer or used in multiple places&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; Lambdas do not work in GlowScript at all. Always use &lt;br /&gt;
&amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when writing code for Trinket.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Inputs and Type Matching&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
All three functions follow the same basic structure:&lt;br /&gt;
&lt;br /&gt;
 function_name(function, list)&lt;br /&gt;
&lt;br /&gt;
The first argument is the function you want to use. The second is the list of &lt;br /&gt;
data you want to run it on, which can be a list, tuple, etc.&lt;br /&gt;
&lt;br /&gt;
One thing to watch out for: the function has to be able to work with any item &lt;br /&gt;
is in the list. If your list has decimal numbers but your function expects &lt;br /&gt;
whole numbers, Python will throw an error.&lt;br /&gt;
&lt;br /&gt;
Example using a named function:&lt;br /&gt;
 def cubed(x):&lt;br /&gt;
     return x**3&lt;br /&gt;
 &lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(cubed, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
The same thing with a lambda:&lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(lambda x: x**3, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
You can also pass Python&#039;s built-in functions straight in:&lt;br /&gt;
 words = [&#039;hello&#039;, &#039;world&#039;, &#039;vpython&#039;]&lt;br /&gt;
 lengths = list(map(len, words))&lt;br /&gt;
 # Result: [5, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Map()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;map(function, list)&amp;lt;/code&amp;gt; runs a function on every single element in a list &lt;br /&gt;
and gives you back all the results. The original list stays the same; you get a &lt;br /&gt;
new list of updated values.&lt;br /&gt;
&lt;br /&gt;
 map(function, list)&lt;br /&gt;
&lt;br /&gt;
So for a list &amp;lt;code&amp;gt;[a, b, c, d]&amp;lt;/code&amp;gt; and some function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;:&lt;br /&gt;
 map(f, [a, b, c, d])  →  [f(a), f(b), f(c), f(d)]&lt;br /&gt;
&lt;br /&gt;
=== Basic example ===&lt;br /&gt;
&lt;br /&gt;
 numlist = [1, 2, 3, 4, 5]&lt;br /&gt;
 result = list(map(lambda x: x * 2, numlist))&lt;br /&gt;
 # Result: [2, 4, 6, 8, 10]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational weight (F = mg) for the masses:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8  # m/s^2&lt;br /&gt;
 masses = [0.5, 1.0, 2.5, 5.0, 10.0]  # kg&lt;br /&gt;
 &lt;br /&gt;
 weights = list(map(lambda m: m * g, masses))&lt;br /&gt;
 # Result: [4.9, 9.8, 24.5, 49.0, 98.0]  Newtons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Kinetic energy of the velocities:&#039;&#039;&#039;&lt;br /&gt;
 mass = 2.0   # kg&lt;br /&gt;
 velocities = [3.0, 5.5, 2.1, 8.0]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 ke_list = list(map(lambda v: 0.5 * mass * v**2, velocities))&lt;br /&gt;
 # Result: [9.0, 30.25, 4.41, 64.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Converting temperature from Celsius to Kelvin:&#039;&#039;&#039;&lt;br /&gt;
 temps_C = [0, 20, 37, 100, -273.15]&lt;br /&gt;
 temps_K = list(map(lambda T: T + 273.15, temps_C))&lt;br /&gt;
 # Result: [273.15, 293.15, 310.15, 373.15, 0.0]  Kelvin&lt;br /&gt;
&lt;br /&gt;
=== map() vs a for-loop ===&lt;br /&gt;
&lt;br /&gt;
Both of these give the same answer, but &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; is more concise:&lt;br /&gt;
&lt;br /&gt;
 # For-loop&lt;br /&gt;
 weights = []&lt;br /&gt;
 for m in masses:&lt;br /&gt;
     weights.append(m * 9.8)&lt;br /&gt;
 &lt;br /&gt;
 # map() — same thing in one line&lt;br /&gt;
 weights = list(map(lambda m: m * 9.8, masses))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Filter()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;filter(function, list)&amp;lt;/code&amp;gt; iterates through a list and only keeps the elements &lt;br /&gt;
where the function returns &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 filter(function, list)&lt;br /&gt;
 filter(lambda x: condition, list)&lt;br /&gt;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
 numbers = [3, 7, 5, 2, 1, 6]&lt;br /&gt;
 result = list(filter(lambda x: x &amp;gt; 3, numbers))&lt;br /&gt;
 # Result: [7, 5, 6]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Retrieving only the fastest particles:&#039;&#039;&#039;&lt;br /&gt;
 speeds = [120, 340, 95, 500, 210, 80]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 fast_particles = list(filter(lambda v: v &amp;gt; 200, speeds))&lt;br /&gt;
 # Result: [340, 500, 210]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Keeping the positive charges:&#039;&#039;&#039;&lt;br /&gt;
 charges = [-1.6e-19, 1.6e-19, -3.2e-19, 3.2e-19, 0, 1.6e-19]  # Coulombs&lt;br /&gt;
 &lt;br /&gt;
 positive = list(filter(lambda q: q &amp;gt; 0, charges))&lt;br /&gt;
 # Result: [1.6e-19, 3.2e-19, 1.6e-19]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Removing particles that exited the simulation boundary:&#039;&#039;&#039;&lt;br /&gt;
 # Each particle p has a .pos.x value for its x position&lt;br /&gt;
 boundary = 10.0  # meters&lt;br /&gt;
 &lt;br /&gt;
 inside = list(filter(lambda p: abs(p.pos.x) &amp;lt; boundary, particles))&lt;br /&gt;
&lt;br /&gt;
=== Passing None as the function ===&lt;br /&gt;
&lt;br /&gt;
If you pass &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; instead of a function, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; removes &lt;br /&gt;
every zero, empty string, &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt; from the list:&lt;br /&gt;
&lt;br /&gt;
 messy = [1, 0, 3, None, 5, 0, 7]&lt;br /&gt;
 clean = list(filter(None, messy))&lt;br /&gt;
 # Result: [1, 3, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;reduce(function, list, starting value)&amp;lt;/code&amp;gt; takes a list and returns a single value. &lt;br /&gt;
&lt;br /&gt;
The function you pass in needs to take two inputs:&lt;br /&gt;
* The running total so far&lt;br /&gt;
* The next item in the list&lt;br /&gt;
&lt;br /&gt;
After each step, the result becomes the new total for the next step.&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 reduce(function, list, starting value)&lt;br /&gt;
&lt;br /&gt;
=== Step-by-step walkthrough ===&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 numbers = [1, 2, 3, 4]&lt;br /&gt;
 result = reduce(lambda x, y: x * y, numbers)&lt;br /&gt;
 # Step 1: x=1, y=2  →  1 * 2 = 2&lt;br /&gt;
 # Step 2: x=2, y=3  →  2 * 3 = 6&lt;br /&gt;
 # Step 3: x=6, y=4  →  6 * 4 = 24&lt;br /&gt;
 # Final result: 24&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Adding up all the masses in a system:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 masses = [1.0, 2.0, 3.0, 4.0]  # kg&lt;br /&gt;
 total_mass = reduce(lambda acc, m: acc + m, masses, 0.0)&lt;br /&gt;
 # Result: 10.0 kg&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Finding the fastest particle in a list:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 speeds = [3.2, 7.8, 1.1, 9.4, 5.5]  # m/s&lt;br /&gt;
 max_speed = reduce(lambda a, b: a if a &amp;gt; b else b, speeds)&lt;br /&gt;
 # Result: 9.4 m/s&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Total work done (W = F·d):&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 forces = [10.0, 25.0, 5.0, 40.0]       # Newtons&lt;br /&gt;
 displacements = [2.0, 1.5, 3.0, 0.5]   # meters&lt;br /&gt;
 &lt;br /&gt;
 work_list = list(map(lambda fd: fd[0] * fd[1], zip(forces, displacements)))&lt;br /&gt;
 total_work = reduce(lambda acc, w: acc + w, work_list, 0.0)&lt;br /&gt;
 # Result: 20.0 + 37.5 + 15.0 + 20.0 = 92.5 Joules&lt;br /&gt;
&lt;br /&gt;
=== Watch out ===&lt;br /&gt;
&lt;br /&gt;
Always give &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; a starting value as the third argument. If the &lt;br /&gt;
list is empty and there is no starting value, Python will crash. With a starting &lt;br /&gt;
value, an empty list will give you that value back instead:&lt;br /&gt;
&lt;br /&gt;
 reduce(lambda acc, x: acc + x, [], 0.0)&lt;br /&gt;
 # Returns 0.0 safely&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Combining map(), filter(), and reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
It is extremely efficient that these functions work well when chained together.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example: kinetic energy&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 # Step 1: filter(): drop all values below 3.0 m/s&lt;br /&gt;
 moving = list(filter(lambda p: p.speed &amp;gt; 3.0, particles))&lt;br /&gt;
 &lt;br /&gt;
 # Step 2: map(): calculate the kinetic energy for each remaining particle&lt;br /&gt;
 ke_list = list(map(lambda p: 0.5 * p.mass * p.speed**2, moving))&lt;br /&gt;
 &lt;br /&gt;
 # Step 3: reduce(): add them all up&lt;br /&gt;
 total_ke = reduce(lambda acc, ke: acc + ke, ke_list, 0.0)&lt;br /&gt;
 print(&amp;quot;Total KE of fast particles:&amp;quot;, round(total_ke, 2), &amp;quot;J&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
This filter → map → reduce pattern shows up very commonly physics simulations. &lt;br /&gt;
Pick a group of objects, apply a formula to each one, then get one final number &lt;br /&gt;
out of it. Below are some examples of these functions chained together.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational potential energy at different heights:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8   # m/s^2&lt;br /&gt;
 m = 2.0   # kg&lt;br /&gt;
 heights = [1.0, 5.0, 10.0, 20.0, 50.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 pe_list = list(map(lambda h: m * g * h, heights))&lt;br /&gt;
 # Result: [19.6, 98.0, 196.0, 392.0, 980.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Electric force on an electron at different distances from a charge (Coulomb&#039;s law):&#039;&#039;&#039;&lt;br /&gt;
 k = 8.99e9   # N·m^2/C^2&lt;br /&gt;
 Q = 1.0e-6   # source charge, Coulombs&lt;br /&gt;
 r_list = [0.1, 0.2, 0.5, 1.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 forces = list(map(lambda r: k * Q * 1.6e-19 / r**2, r_list))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Interactive Simulation&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
The following GlowScript simulation shows all three functions working together &lt;br /&gt;
in a real physics example:&lt;br /&gt;
[https://trinket.io/glowscript/9e53d57b352f map(), filter(), and reduce() in VPython Physics — Trinket]&lt;br /&gt;
&lt;br /&gt;
The simulation puts five spheres in a row, each with a different mass between &lt;br /&gt;
1 and 8 kg and a different speed between 1.5 and 6 m/s. The size of each sphere &lt;br /&gt;
matches its mass so you can see the difference right away.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;map()&#039;&#039;&#039; goes through every mass and calculates the weight using F = mg. &lt;br /&gt;
The weight of each sphere gets printed to the console.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;filter()&#039;&#039;&#039; checks each sphere&#039;s speed and keeps the ones faster &lt;br /&gt;
than 3.0 m/s. &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;reduce()&#039;&#039;&#039; is written by hand since GlowScript does not have the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. It adds up the kinetic energy of every sphere &lt;br /&gt;
one by one until it has one total number for the whole system, which then &lt;br /&gt;
gets printed.&lt;br /&gt;
&lt;br /&gt;
This simulation is an accurate visualization of how all three functions work together.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;References&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
[http://www.bogotobogo.com/python/python_fncs_map_filter_reduce.php 1. Python map, filter, reduce — bogotobogo.com]&lt;br /&gt;
&lt;br /&gt;
[http://book.pythontips.com/en/latest/map_filter.html 2. Map, Filter — Python Tips]&lt;br /&gt;
&lt;br /&gt;
[http://vpython.org/contents/docs/VisualIntro.html 3. VPython Documentation — vpython.org]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functions.html 4. Python 3 Built-in Functions (map, filter) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functools.html 5. functools module (reduce) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/howto/functional.html 6. Functional Programming HOWTO — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-map-function/ 7. Python&#039;s map() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-filter-function/ 8. Python&#039;s filter() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-reduce-function/ 9. Python&#039;s reduce() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-lambda/ 10. Lambda Expressions in Python — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/functional-programming-in-python/ 11. Functional Programming in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/higher-order-functions-in-python/ 12. Higher-Order Functions in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.glowscript.org 13. GlowScript VPython — Official Site]&lt;br /&gt;
&lt;br /&gt;
[https://trinket.io/glowscript 14. GlowScript on Trinket]&lt;br /&gt;
&lt;br /&gt;
[https://vpython.org 15. VPython Official Documentation]&lt;/div&gt;</summary>
		<author><name>Sabrinayang</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48099</id>
		<title>VPython MapReduceFilter</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48099"/>
		<updated>2026-04-27T00:58:30Z</updated>

		<summary type="html">&lt;p&gt;Sabrinayang: /* Reduce() */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;&amp;lt;big&amp;gt;Sabrina Yang - Spring 2026&amp;lt;/big&amp;gt;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Introduction&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; are &lt;br /&gt;
known as higher-order functions. Higher-order functions are functions that &lt;br /&gt;
take other functions as arguments. VPython simulations usually have hundreds of &lt;br /&gt;
objects that all follow the same physics equations, and writing for-loops to do the same &lt;br /&gt;
calculation over and over can be inefficient. &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &lt;br /&gt;
&amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; take care of the for-loops, &lt;br /&gt;
allowing you to focus on the point of the code itself.&lt;br /&gt;
&lt;br /&gt;
Each function does something different:&lt;br /&gt;
* &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;: applies a function to every element in a list&lt;br /&gt;
* &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;: iterates through a list and only keeps the elements that meet a certain condition&lt;br /&gt;
* &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;: takes a whole list and turns it into one single value&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Python 3 note:&#039;&#039;&#039; &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; do not execute until you ask for the results. Wrap them in &amp;lt;code&amp;gt;list()&amp;lt;/code&amp;gt; to get &lt;br /&gt;
the results. &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; has to be imported before you can use it:&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; GlowScript does not support lambda expressions or the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. Use regular &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; functions instead of &lt;br /&gt;
lambdas, and write &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; yourself. Here is an example of this &lt;br /&gt;
in the simulation section below.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Background&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, functions are objects, meaning you can:&lt;br /&gt;
* Store a function in a variable&lt;br /&gt;
* Pass a function into another function as an argument&lt;br /&gt;
* Get a function back as a return value&lt;br /&gt;
&lt;br /&gt;
A few reasons why writing code this way is efficient:&lt;br /&gt;
* &#039;&#039;&#039;It is easy to read:&#039;&#039;&#039; You can immediately tell what the program &lt;br /&gt;
is doing. It is obvious that the program is saying to &amp;quot;apply this formula to every mass.&amp;quot; &lt;br /&gt;
There is no complex decoding required.&lt;br /&gt;
* &#039;&#039;&#039;Fewer bugs:&#039;&#039;&#039; Every time you write a for-loop you have to manually keep track of index &lt;br /&gt;
variables, which is easy to make mistakes. These functions &lt;br /&gt;
handle the indices, so there is less room for errors.&lt;br /&gt;
* &#039;&#039;&#039;You can chain them:&#039;&#039;&#039; The output of &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; goes straight into &lt;br /&gt;
&amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; without extra steps, which keeps &lt;br /&gt;
the variables clean and short.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Lambda Expressions&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
A &#039;&#039;&#039;lambda expression&#039;&#039;&#039; is a function you write in one line without giving it a name. &lt;br /&gt;
The format is:&lt;br /&gt;
&lt;br /&gt;
 lambda &amp;lt;parameters&amp;gt;: &amp;lt;expression&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function only exists at the point where it is used. These two do the exact same thing:&lt;br /&gt;
&lt;br /&gt;
 # Regular way&lt;br /&gt;
 def square(x):&lt;br /&gt;
     return x**2&lt;br /&gt;
 &lt;br /&gt;
 # Lambda way&lt;br /&gt;
 square = lambda x: x**2&lt;br /&gt;
&lt;br /&gt;
Lambdas save you from having to define a whole separate function when you only &lt;br /&gt;
need it once:&lt;br /&gt;
&lt;br /&gt;
 # Without lambda: needs a function defined somewhere else&lt;br /&gt;
 result = list(map(square, [1, 2, 3, 4]))&lt;br /&gt;
 &lt;br /&gt;
 # With lambda: no separate function needed&lt;br /&gt;
 result = list(map(lambda x: x**2, [1, 2, 3, 4]))&lt;br /&gt;
 # Result: [1, 4, 9, 16]&lt;br /&gt;
&lt;br /&gt;
They can also take two inputs, which comes up when using &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 total = reduce(lambda acc, x: acc + x, [1, 2, 3, 4], 0)&lt;br /&gt;
 # Result: 10&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lambda vs def — when to use which:&#039;&#039;&#039;&lt;br /&gt;
* Use a lambda when the function is short, only needed once, and passed right &lt;br /&gt;
into another function&lt;br /&gt;
* Use &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when the function is longer or used in multiple places&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; Lambdas do not work in GlowScript at all. Always use &lt;br /&gt;
&amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when writing code for Trinket.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Inputs and Type Matching&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
All three functions follow the same basic structure:&lt;br /&gt;
&lt;br /&gt;
 function_name(function, list)&lt;br /&gt;
&lt;br /&gt;
The first argument is the function you want to use. The second is the list of &lt;br /&gt;
data you want to run it on, which can be a list, tuple, etc.&lt;br /&gt;
&lt;br /&gt;
One thing to watch out for: the function has to be able to work with any item &lt;br /&gt;
is in the list. If your list has decimal numbers but your function expects &lt;br /&gt;
whole numbers, Python will throw an error.&lt;br /&gt;
&lt;br /&gt;
Example using a named function:&lt;br /&gt;
 def cubed(x):&lt;br /&gt;
     return x**3&lt;br /&gt;
 &lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(cubed, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
The same thing with a lambda:&lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(lambda x: x**3, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
You can also pass Python&#039;s built-in functions straight in:&lt;br /&gt;
 words = [&#039;hello&#039;, &#039;world&#039;, &#039;vpython&#039;]&lt;br /&gt;
 lengths = list(map(len, words))&lt;br /&gt;
 # Result: [5, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Map()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;map(function, list)&amp;lt;/code&amp;gt; runs a function on every single element in a list &lt;br /&gt;
and gives you back all the results. The original list stays the same; you get a &lt;br /&gt;
new list of updated values.&lt;br /&gt;
&lt;br /&gt;
 map(function, list)&lt;br /&gt;
&lt;br /&gt;
So for a list &amp;lt;code&amp;gt;[a, b, c, d]&amp;lt;/code&amp;gt; and some function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;:&lt;br /&gt;
 map(f, [a, b, c, d])  →  [f(a), f(b), f(c), f(d)]&lt;br /&gt;
&lt;br /&gt;
=== Basic example ===&lt;br /&gt;
&lt;br /&gt;
 numlist = [1, 2, 3, 4, 5]&lt;br /&gt;
 result = list(map(lambda x: x * 2, numlist))&lt;br /&gt;
 # Result: [2, 4, 6, 8, 10]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational weight (F = mg) for the masses:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8  # m/s^2&lt;br /&gt;
 masses = [0.5, 1.0, 2.5, 5.0, 10.0]  # kg&lt;br /&gt;
 &lt;br /&gt;
 weights = list(map(lambda m: m * g, masses))&lt;br /&gt;
 # Result: [4.9, 9.8, 24.5, 49.0, 98.0]  Newtons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Kinetic energy of the velocities:&#039;&#039;&#039;&lt;br /&gt;
 mass = 2.0   # kg&lt;br /&gt;
 velocities = [3.0, 5.5, 2.1, 8.0]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 ke_list = list(map(lambda v: 0.5 * mass * v**2, velocities))&lt;br /&gt;
 # Result: [9.0, 30.25, 4.41, 64.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Converting temperature from Celsius to Kelvin:&#039;&#039;&#039;&lt;br /&gt;
 temps_C = [0, 20, 37, 100, -273.15]&lt;br /&gt;
 temps_K = list(map(lambda T: T + 273.15, temps_C))&lt;br /&gt;
 # Result: [273.15, 293.15, 310.15, 373.15, 0.0]  Kelvin&lt;br /&gt;
&lt;br /&gt;
=== map() vs a for-loop ===&lt;br /&gt;
&lt;br /&gt;
Both of these give the same answer, but &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; is more concise:&lt;br /&gt;
&lt;br /&gt;
 # For-loop&lt;br /&gt;
 weights = []&lt;br /&gt;
 for m in masses:&lt;br /&gt;
     weights.append(m * 9.8)&lt;br /&gt;
 &lt;br /&gt;
 # map() — same thing in one line&lt;br /&gt;
 weights = list(map(lambda m: m * 9.8, masses))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Filter()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;filter(function, list)&amp;lt;/code&amp;gt; iterates through a list and only keeps the elements &lt;br /&gt;
where the function returns &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 filter(function, list)&lt;br /&gt;
 filter(lambda x: condition, list)&lt;br /&gt;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
 numbers = [3, 7, 5, 2, 1, 6]&lt;br /&gt;
 result = list(filter(lambda x: x &amp;gt; 3, numbers))&lt;br /&gt;
 # Result: [7, 5, 6]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Retrieving only the fastest particles:&#039;&#039;&#039;&lt;br /&gt;
 speeds = [120, 340, 95, 500, 210, 80]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 fast_particles = list(filter(lambda v: v &amp;gt; 200, speeds))&lt;br /&gt;
 # Result: [340, 500, 210]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Keeping the positive charges:&#039;&#039;&#039;&lt;br /&gt;
 charges = [-1.6e-19, 1.6e-19, -3.2e-19, 3.2e-19, 0, 1.6e-19]  # Coulombs&lt;br /&gt;
 &lt;br /&gt;
 positive = list(filter(lambda q: q &amp;gt; 0, charges))&lt;br /&gt;
 # Result: [1.6e-19, 3.2e-19, 1.6e-19]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Removing particles that exited the simulation boundary:&#039;&#039;&#039;&lt;br /&gt;
 # Each particle p has a .pos.x value for its x position&lt;br /&gt;
 boundary = 10.0  # meters&lt;br /&gt;
 &lt;br /&gt;
 inside = list(filter(lambda p: abs(p.pos.x) &amp;lt; boundary, particles))&lt;br /&gt;
&lt;br /&gt;
=== Passing None as the function ===&lt;br /&gt;
&lt;br /&gt;
If you pass &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; instead of a function, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; removes &lt;br /&gt;
every zero, empty string, &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt; from the list:&lt;br /&gt;
&lt;br /&gt;
 messy = [1, 0, 3, None, 5, 0, 7]&lt;br /&gt;
 clean = list(filter(None, messy))&lt;br /&gt;
 # Result: [1, 3, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;reduce(function, list, starting value)&amp;lt;/code&amp;gt; takes a list and returns a single value. &lt;br /&gt;
&lt;br /&gt;
The function you pass in needs to take two inputs:&lt;br /&gt;
* The running total so far&lt;br /&gt;
* The next item in the list&lt;br /&gt;
&lt;br /&gt;
After each step, the result becomes the new total for the next step.&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 reduce(function, list, starting value)&lt;br /&gt;
&lt;br /&gt;
=== Step-by-step walkthrough ===&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 numbers = [1, 2, 3, 4]&lt;br /&gt;
 result = reduce(lambda x, y: x * y, numbers)&lt;br /&gt;
 # Step 1: x=1, y=2  →  1 * 2 = 2&lt;br /&gt;
 # Step 2: x=2, y=3  →  2 * 3 = 6&lt;br /&gt;
 # Step 3: x=6, y=4  →  6 * 4 = 24&lt;br /&gt;
 # Final result: 24&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Adding up all the masses in a system:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 masses = [1.0, 2.0, 3.0, 4.0]  # kg&lt;br /&gt;
 total_mass = reduce(lambda acc, m: acc + m, masses, 0.0)&lt;br /&gt;
 # Result: 10.0 kg&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Finding the fastest particle in a list:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 speeds = [3.2, 7.8, 1.1, 9.4, 5.5]  # m/s&lt;br /&gt;
 max_speed = reduce(lambda a, b: a if a &amp;gt; b else b, speeds)&lt;br /&gt;
 # Result: 9.4 m/s&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Total work done (W = F·d):&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 forces = [10.0, 25.0, 5.0, 40.0]       # Newtons&lt;br /&gt;
 displacements = [2.0, 1.5, 3.0, 0.5]   # meters&lt;br /&gt;
 &lt;br /&gt;
 work_list = list(map(lambda fd: fd[0] * fd[1], zip(forces, displacements)))&lt;br /&gt;
 total_work = reduce(lambda acc, w: acc + w, work_list, 0.0)&lt;br /&gt;
 # Result: 20.0 + 37.5 + 15.0 + 20.0 = 92.5 Joules&lt;br /&gt;
&lt;br /&gt;
=== Watch out ===&lt;br /&gt;
&lt;br /&gt;
Always give &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; a starting value as the third argument. If the &lt;br /&gt;
list is empty and there is no starting value, Python will crash. With a starting &lt;br /&gt;
value, an empty list will give you that value back instead:&lt;br /&gt;
&lt;br /&gt;
 reduce(lambda acc, x: acc + x, [], 0.0)&lt;br /&gt;
 # Returns 0.0 safely&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Combining map(), filter(), and reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
It is also efficient that functions can be chained together.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example: kinetic energy&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 # Step 1: filter() — drop all values below 3.0 m/s&lt;br /&gt;
 moving = list(filter(lambda p: p.speed &amp;gt; 3.0, particles))&lt;br /&gt;
 &lt;br /&gt;
 # Step 2: map() — calculate KE for each remaining particle&lt;br /&gt;
 ke_list = list(map(lambda p: 0.5 * p.mass * p.speed**2, moving))&lt;br /&gt;
 &lt;br /&gt;
 # Step 3: reduce() — add them all up&lt;br /&gt;
 total_ke = reduce(lambda acc, ke: acc + ke, ke_list, 0.0)&lt;br /&gt;
 print(&amp;quot;Total KE of fast particles:&amp;quot;, round(total_ke, 2), &amp;quot;J&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
This filter → map → reduce pattern shows up all the time in physics simulations. &lt;br /&gt;
Pick a group of objects, apply a formula to each one, then get one final number &lt;br /&gt;
out of it. That is basically what these three functions are built for.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational potential energy at different heights:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8   # m/s^2&lt;br /&gt;
 m = 2.0   # kg&lt;br /&gt;
 heights = [1.0, 5.0, 10.0, 20.0, 50.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 pe_list = list(map(lambda h: m * g * h, heights))&lt;br /&gt;
 # Result: [19.6, 98.0, 196.0, 392.0, 980.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Electric force on an electron at different distances from a charge (Coulomb&#039;s law):&#039;&#039;&#039;&lt;br /&gt;
 k = 8.99e9   # N·m^2/C^2&lt;br /&gt;
 Q = 1.0e-6   # source charge, Coulombs&lt;br /&gt;
 r_list = [0.1, 0.2, 0.5, 1.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 forces = list(map(lambda r: k * Q * 1.6e-19 / r**2, r_list))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Interactive Simulation&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
The following GlowScript simulation shows all three functions working together &lt;br /&gt;
in a real physics example:&lt;br /&gt;
[https://trinket.io/glowscript/9e53d57b352f map(), filter(), and reduce() in VPython Physics — Trinket]&lt;br /&gt;
&lt;br /&gt;
The simulation puts five spheres in a row, each with a different mass between &lt;br /&gt;
1 and 8 kg and a different speed between 1.5 and 6 m/s. The size of each sphere &lt;br /&gt;
matches its mass so you can see the difference right away.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;map()&#039;&#039;&#039; goes through every mass and calculates the weight using F = mg. &lt;br /&gt;
The weight of each sphere gets printed to the console.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;filter()&#039;&#039;&#039; checks each sphere&#039;s speed and keeps the ones faster &lt;br /&gt;
than 3.0 m/s. &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;reduce()&#039;&#039;&#039; is written by hand since GlowScript does not have the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. It adds up the kinetic energy of every sphere &lt;br /&gt;
one by one until it has one total number for the whole system, which then &lt;br /&gt;
gets printed.&lt;br /&gt;
&lt;br /&gt;
This simulation is an accurate visualization of how all three functions work together.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;References&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
[http://www.bogotobogo.com/python/python_fncs_map_filter_reduce.php 1. Python map, filter, reduce — bogotobogo.com]&lt;br /&gt;
&lt;br /&gt;
[http://book.pythontips.com/en/latest/map_filter.html 2. Map, Filter — Python Tips]&lt;br /&gt;
&lt;br /&gt;
[http://vpython.org/contents/docs/VisualIntro.html 3. VPython Documentation — vpython.org]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functions.html 4. Python 3 Built-in Functions (map, filter) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functools.html 5. functools module (reduce) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/howto/functional.html 6. Functional Programming HOWTO — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-map-function/ 7. Python&#039;s map() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-filter-function/ 8. Python&#039;s filter() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-reduce-function/ 9. Python&#039;s reduce() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-lambda/ 10. Lambda Expressions in Python — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/functional-programming-in-python/ 11. Functional Programming in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/higher-order-functions-in-python/ 12. Higher-Order Functions in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.glowscript.org 13. GlowScript VPython — Official Site]&lt;br /&gt;
&lt;br /&gt;
[https://trinket.io/glowscript 14. GlowScript on Trinket]&lt;br /&gt;
&lt;br /&gt;
[https://vpython.org 15. VPython Official Documentation]&lt;/div&gt;</summary>
		<author><name>Sabrinayang</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48098</id>
		<title>VPython MapReduceFilter</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48098"/>
		<updated>2026-04-27T00:56:45Z</updated>

		<summary type="html">&lt;p&gt;Sabrinayang: /* Background */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;&amp;lt;big&amp;gt;Sabrina Yang - Spring 2026&amp;lt;/big&amp;gt;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Introduction&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; are &lt;br /&gt;
known as higher-order functions. Higher-order functions are functions that &lt;br /&gt;
take other functions as arguments. VPython simulations usually have hundreds of &lt;br /&gt;
objects that all follow the same physics equations, and writing for-loops to do the same &lt;br /&gt;
calculation over and over can be inefficient. &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &lt;br /&gt;
&amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; take care of the for-loops, &lt;br /&gt;
allowing you to focus on the point of the code itself.&lt;br /&gt;
&lt;br /&gt;
Each function does something different:&lt;br /&gt;
* &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;: applies a function to every element in a list&lt;br /&gt;
* &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;: iterates through a list and only keeps the elements that meet a certain condition&lt;br /&gt;
* &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;: takes a whole list and turns it into one single value&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Python 3 note:&#039;&#039;&#039; &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; do not execute until you ask for the results. Wrap them in &amp;lt;code&amp;gt;list()&amp;lt;/code&amp;gt; to get &lt;br /&gt;
the results. &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; has to be imported before you can use it:&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; GlowScript does not support lambda expressions or the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. Use regular &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; functions instead of &lt;br /&gt;
lambdas, and write &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; yourself. Here is an example of this &lt;br /&gt;
in the simulation section below.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Background&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, functions are objects, meaning you can:&lt;br /&gt;
* Store a function in a variable&lt;br /&gt;
* Pass a function into another function as an argument&lt;br /&gt;
* Get a function back as a return value&lt;br /&gt;
&lt;br /&gt;
A few reasons why writing code this way is efficient:&lt;br /&gt;
* &#039;&#039;&#039;It is easy to read:&#039;&#039;&#039; You can immediately tell what the program &lt;br /&gt;
is doing. It is obvious that the program is saying to &amp;quot;apply this formula to every mass.&amp;quot; &lt;br /&gt;
There is no complex decoding required.&lt;br /&gt;
* &#039;&#039;&#039;Fewer bugs:&#039;&#039;&#039; Every time you write a for-loop you have to manually keep track of index &lt;br /&gt;
variables, which is easy to make mistakes. These functions &lt;br /&gt;
handle the indices, so there is less room for errors.&lt;br /&gt;
* &#039;&#039;&#039;You can chain them:&#039;&#039;&#039; The output of &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; goes straight into &lt;br /&gt;
&amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; without extra steps, which keeps &lt;br /&gt;
the variables clean and short.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Lambda Expressions&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
A &#039;&#039;&#039;lambda expression&#039;&#039;&#039; is a function you write in one line without giving it a name. &lt;br /&gt;
The format is:&lt;br /&gt;
&lt;br /&gt;
 lambda &amp;lt;parameters&amp;gt;: &amp;lt;expression&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function only exists at the point where it is used. These two do the exact same thing:&lt;br /&gt;
&lt;br /&gt;
 # Regular way&lt;br /&gt;
 def square(x):&lt;br /&gt;
     return x**2&lt;br /&gt;
 &lt;br /&gt;
 # Lambda way&lt;br /&gt;
 square = lambda x: x**2&lt;br /&gt;
&lt;br /&gt;
Lambdas save you from having to define a whole separate function when you only &lt;br /&gt;
need it once:&lt;br /&gt;
&lt;br /&gt;
 # Without lambda: needs a function defined somewhere else&lt;br /&gt;
 result = list(map(square, [1, 2, 3, 4]))&lt;br /&gt;
 &lt;br /&gt;
 # With lambda: no separate function needed&lt;br /&gt;
 result = list(map(lambda x: x**2, [1, 2, 3, 4]))&lt;br /&gt;
 # Result: [1, 4, 9, 16]&lt;br /&gt;
&lt;br /&gt;
They can also take two inputs, which comes up when using &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 total = reduce(lambda acc, x: acc + x, [1, 2, 3, 4], 0)&lt;br /&gt;
 # Result: 10&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lambda vs def — when to use which:&#039;&#039;&#039;&lt;br /&gt;
* Use a lambda when the function is short, only needed once, and passed right &lt;br /&gt;
into another function&lt;br /&gt;
* Use &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when the function is longer or used in multiple places&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; Lambdas do not work in GlowScript at all. Always use &lt;br /&gt;
&amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when writing code for Trinket.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Inputs and Type Matching&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
All three functions follow the same basic structure:&lt;br /&gt;
&lt;br /&gt;
 function_name(function, list)&lt;br /&gt;
&lt;br /&gt;
The first argument is the function you want to use. The second is the list of &lt;br /&gt;
data you want to run it on, which can be a list, tuple, etc.&lt;br /&gt;
&lt;br /&gt;
One thing to watch out for: the function has to be able to work with any item &lt;br /&gt;
is in the list. If your list has decimal numbers but your function expects &lt;br /&gt;
whole numbers, Python will throw an error.&lt;br /&gt;
&lt;br /&gt;
Example using a named function:&lt;br /&gt;
 def cubed(x):&lt;br /&gt;
     return x**3&lt;br /&gt;
 &lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(cubed, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
The same thing with a lambda:&lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(lambda x: x**3, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
You can also pass Python&#039;s built-in functions straight in:&lt;br /&gt;
 words = [&#039;hello&#039;, &#039;world&#039;, &#039;vpython&#039;]&lt;br /&gt;
 lengths = list(map(len, words))&lt;br /&gt;
 # Result: [5, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Map()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;map(function, list)&amp;lt;/code&amp;gt; runs a function on every single element in a list &lt;br /&gt;
and gives you back all the results. The original list stays the same; you get a &lt;br /&gt;
new list of updated values.&lt;br /&gt;
&lt;br /&gt;
 map(function, list)&lt;br /&gt;
&lt;br /&gt;
So for a list &amp;lt;code&amp;gt;[a, b, c, d]&amp;lt;/code&amp;gt; and some function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;:&lt;br /&gt;
 map(f, [a, b, c, d])  →  [f(a), f(b), f(c), f(d)]&lt;br /&gt;
&lt;br /&gt;
=== Basic example ===&lt;br /&gt;
&lt;br /&gt;
 numlist = [1, 2, 3, 4, 5]&lt;br /&gt;
 result = list(map(lambda x: x * 2, numlist))&lt;br /&gt;
 # Result: [2, 4, 6, 8, 10]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational weight (F = mg) for the masses:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8  # m/s^2&lt;br /&gt;
 masses = [0.5, 1.0, 2.5, 5.0, 10.0]  # kg&lt;br /&gt;
 &lt;br /&gt;
 weights = list(map(lambda m: m * g, masses))&lt;br /&gt;
 # Result: [4.9, 9.8, 24.5, 49.0, 98.0]  Newtons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Kinetic energy of the velocities:&#039;&#039;&#039;&lt;br /&gt;
 mass = 2.0   # kg&lt;br /&gt;
 velocities = [3.0, 5.5, 2.1, 8.0]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 ke_list = list(map(lambda v: 0.5 * mass * v**2, velocities))&lt;br /&gt;
 # Result: [9.0, 30.25, 4.41, 64.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Converting temperature from Celsius to Kelvin:&#039;&#039;&#039;&lt;br /&gt;
 temps_C = [0, 20, 37, 100, -273.15]&lt;br /&gt;
 temps_K = list(map(lambda T: T + 273.15, temps_C))&lt;br /&gt;
 # Result: [273.15, 293.15, 310.15, 373.15, 0.0]  Kelvin&lt;br /&gt;
&lt;br /&gt;
=== map() vs a for-loop ===&lt;br /&gt;
&lt;br /&gt;
Both of these give the same answer, but &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; is more concise:&lt;br /&gt;
&lt;br /&gt;
 # For-loop&lt;br /&gt;
 weights = []&lt;br /&gt;
 for m in masses:&lt;br /&gt;
     weights.append(m * 9.8)&lt;br /&gt;
 &lt;br /&gt;
 # map() — same thing in one line&lt;br /&gt;
 weights = list(map(lambda m: m * 9.8, masses))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Filter()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;filter(function, list)&amp;lt;/code&amp;gt; iterates through a list and only keeps the elements &lt;br /&gt;
where the function returns &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 filter(function, list)&lt;br /&gt;
 filter(lambda x: condition, list)&lt;br /&gt;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
 numbers = [3, 7, 5, 2, 1, 6]&lt;br /&gt;
 result = list(filter(lambda x: x &amp;gt; 3, numbers))&lt;br /&gt;
 # Result: [7, 5, 6]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Retrieving only the fastest particles:&#039;&#039;&#039;&lt;br /&gt;
 speeds = [120, 340, 95, 500, 210, 80]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 fast_particles = list(filter(lambda v: v &amp;gt; 200, speeds))&lt;br /&gt;
 # Result: [340, 500, 210]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Keeping the positive charges:&#039;&#039;&#039;&lt;br /&gt;
 charges = [-1.6e-19, 1.6e-19, -3.2e-19, 3.2e-19, 0, 1.6e-19]  # Coulombs&lt;br /&gt;
 &lt;br /&gt;
 positive = list(filter(lambda q: q &amp;gt; 0, charges))&lt;br /&gt;
 # Result: [1.6e-19, 3.2e-19, 1.6e-19]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Removing particles that exited the simulation boundary:&#039;&#039;&#039;&lt;br /&gt;
 # Each particle p has a .pos.x value for its x position&lt;br /&gt;
 boundary = 10.0  # meters&lt;br /&gt;
 &lt;br /&gt;
 inside = list(filter(lambda p: abs(p.pos.x) &amp;lt; boundary, particles))&lt;br /&gt;
&lt;br /&gt;
=== Passing None as the function ===&lt;br /&gt;
&lt;br /&gt;
If you pass &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; instead of a function, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; removes &lt;br /&gt;
every zero, empty string, &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt; from the list:&lt;br /&gt;
&lt;br /&gt;
 messy = [1, 0, 3, None, 5, 0, 7]&lt;br /&gt;
 clean = list(filter(None, messy))&lt;br /&gt;
 # Result: [1, 3, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;reduce(function, list, starting value)&amp;lt;/code&amp;gt; takes a whole list and returns one single value. Unlike &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; &lt;br /&gt;
and &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, you do not get a list back — you get one number.&lt;br /&gt;
&lt;br /&gt;
The function you pass in needs to take two inputs:&lt;br /&gt;
* The running total so far&lt;br /&gt;
* The next item in the list&lt;br /&gt;
&lt;br /&gt;
After each step, the result becomes the new total for the next step.&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 reduce(function, list, starting value)&lt;br /&gt;
&lt;br /&gt;
=== Step-by-step walkthrough ===&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 numbers = [1, 2, 3, 4]&lt;br /&gt;
 result = reduce(lambda x, y: x * y, numbers)&lt;br /&gt;
 # Step 1: x=1, y=2  →  1 * 2 = 2&lt;br /&gt;
 # Step 2: x=2, y=3  →  2 * 3 = 6&lt;br /&gt;
 # Step 3: x=6, y=4  →  6 * 4 = 24&lt;br /&gt;
 # Final result: 24&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Adding up all the masses in a system:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 masses = [1.0, 2.0, 3.0, 4.0]  # kg&lt;br /&gt;
 total_mass = reduce(lambda acc, m: acc + m, masses, 0.0)&lt;br /&gt;
 # Result: 10.0 kg&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Finding the fastest particle in a list:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 speeds = [3.2, 7.8, 1.1, 9.4, 5.5]  # m/s&lt;br /&gt;
 max_speed = reduce(lambda a, b: a if a &amp;gt; b else b, speeds)&lt;br /&gt;
 # Result: 9.4 m/s&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Total work done (W = F·d):&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 forces = [10.0, 25.0, 5.0, 40.0]       # Newtons&lt;br /&gt;
 displacements = [2.0, 1.5, 3.0, 0.5]   # meters&lt;br /&gt;
 &lt;br /&gt;
 work_list = list(map(lambda fd: fd[0] * fd[1], zip(forces, displacements)))&lt;br /&gt;
 total_work = reduce(lambda acc, w: acc + w, work_list, 0.0)&lt;br /&gt;
 # Result: 20.0 + 37.5 + 15.0 + 20.0 = 92.5 Joules&lt;br /&gt;
&lt;br /&gt;
=== Watch out ===&lt;br /&gt;
&lt;br /&gt;
Always give &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; a starting value as the third argument. If the &lt;br /&gt;
list is empty and there is no starting value, Python will crash. With a starting &lt;br /&gt;
value, an empty list will give you that value back instead:&lt;br /&gt;
&lt;br /&gt;
 reduce(lambda acc, x: acc + x, [], 0.0)&lt;br /&gt;
 # Returns 0.0 safely&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Combining map(), filter(), and reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
It is also efficient that functions can be chained together.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example: kinetic energy&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 # Step 1: filter() — drop all values below 3.0 m/s&lt;br /&gt;
 moving = list(filter(lambda p: p.speed &amp;gt; 3.0, particles))&lt;br /&gt;
 &lt;br /&gt;
 # Step 2: map() — calculate KE for each remaining particle&lt;br /&gt;
 ke_list = list(map(lambda p: 0.5 * p.mass * p.speed**2, moving))&lt;br /&gt;
 &lt;br /&gt;
 # Step 3: reduce() — add them all up&lt;br /&gt;
 total_ke = reduce(lambda acc, ke: acc + ke, ke_list, 0.0)&lt;br /&gt;
 print(&amp;quot;Total KE of fast particles:&amp;quot;, round(total_ke, 2), &amp;quot;J&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
This filter → map → reduce pattern shows up all the time in physics simulations. &lt;br /&gt;
Pick a group of objects, apply a formula to each one, then get one final number &lt;br /&gt;
out of it. That is basically what these three functions are built for.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational potential energy at different heights:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8   # m/s^2&lt;br /&gt;
 m = 2.0   # kg&lt;br /&gt;
 heights = [1.0, 5.0, 10.0, 20.0, 50.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 pe_list = list(map(lambda h: m * g * h, heights))&lt;br /&gt;
 # Result: [19.6, 98.0, 196.0, 392.0, 980.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Electric force on an electron at different distances from a charge (Coulomb&#039;s law):&#039;&#039;&#039;&lt;br /&gt;
 k = 8.99e9   # N·m^2/C^2&lt;br /&gt;
 Q = 1.0e-6   # source charge, Coulombs&lt;br /&gt;
 r_list = [0.1, 0.2, 0.5, 1.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 forces = list(map(lambda r: k * Q * 1.6e-19 / r**2, r_list))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Interactive Simulation&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
The following GlowScript simulation shows all three functions working together &lt;br /&gt;
in a real physics example:&lt;br /&gt;
[https://trinket.io/glowscript/9e53d57b352f map(), filter(), and reduce() in VPython Physics — Trinket]&lt;br /&gt;
&lt;br /&gt;
The simulation puts five spheres in a row, each with a different mass between &lt;br /&gt;
1 and 8 kg and a different speed between 1.5 and 6 m/s. The size of each sphere &lt;br /&gt;
matches its mass so you can see the difference right away.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;map()&#039;&#039;&#039; goes through every mass and calculates the weight using F = mg. &lt;br /&gt;
The weight of each sphere gets printed to the console.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;filter()&#039;&#039;&#039; checks each sphere&#039;s speed and keeps the ones faster &lt;br /&gt;
than 3.0 m/s. &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;reduce()&#039;&#039;&#039; is written by hand since GlowScript does not have the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. It adds up the kinetic energy of every sphere &lt;br /&gt;
one by one until it has one total number for the whole system, which then &lt;br /&gt;
gets printed.&lt;br /&gt;
&lt;br /&gt;
This simulation is an accurate visualization of how all three functions work together.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;References&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
[http://www.bogotobogo.com/python/python_fncs_map_filter_reduce.php 1. Python map, filter, reduce — bogotobogo.com]&lt;br /&gt;
&lt;br /&gt;
[http://book.pythontips.com/en/latest/map_filter.html 2. Map, Filter — Python Tips]&lt;br /&gt;
&lt;br /&gt;
[http://vpython.org/contents/docs/VisualIntro.html 3. VPython Documentation — vpython.org]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functions.html 4. Python 3 Built-in Functions (map, filter) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functools.html 5. functools module (reduce) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/howto/functional.html 6. Functional Programming HOWTO — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-map-function/ 7. Python&#039;s map() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-filter-function/ 8. Python&#039;s filter() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-reduce-function/ 9. Python&#039;s reduce() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-lambda/ 10. Lambda Expressions in Python — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/functional-programming-in-python/ 11. Functional Programming in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/higher-order-functions-in-python/ 12. Higher-Order Functions in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.glowscript.org 13. GlowScript VPython — Official Site]&lt;br /&gt;
&lt;br /&gt;
[https://trinket.io/glowscript 14. GlowScript on Trinket]&lt;br /&gt;
&lt;br /&gt;
[https://vpython.org 15. VPython Official Documentation]&lt;/div&gt;</summary>
		<author><name>Sabrinayang</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48093</id>
		<title>VPython MapReduceFilter</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48093"/>
		<updated>2026-04-27T00:51:57Z</updated>

		<summary type="html">&lt;p&gt;Sabrinayang: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;&amp;lt;big&amp;gt;Sabrina Yang - Spring 2026&amp;lt;/big&amp;gt;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Introduction&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; are &lt;br /&gt;
known as higher-order functions. Higher-order functions are functions that &lt;br /&gt;
take other functions as arguments. VPython simulations usually have hundreds of &lt;br /&gt;
objects that all follow the same physics equations, and writing for-loops to do the same &lt;br /&gt;
calculation over and over can be inefficient. &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &lt;br /&gt;
&amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; take care of the for-loops, &lt;br /&gt;
allowing you to focus on the point of the code itself.&lt;br /&gt;
&lt;br /&gt;
Each function does something different:&lt;br /&gt;
* &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;: applies a function to every element in a list&lt;br /&gt;
* &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;: iterates through a list and only keeps the elements that meet a certain condition&lt;br /&gt;
* &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;: takes a whole list and turns it into one single value&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Python 3 note:&#039;&#039;&#039; &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; do not execute until you ask for the results. Wrap them in &amp;lt;code&amp;gt;list()&amp;lt;/code&amp;gt; to get &lt;br /&gt;
the results. &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; has to be imported before you can use it:&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; GlowScript does not support lambda expressions or the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. Use regular &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; functions instead of &lt;br /&gt;
lambdas, and write &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; yourself. Here is an example of this &lt;br /&gt;
in the simulation section below.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Background&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, functions are objects just like numbers or strings. That means you can:&lt;br /&gt;
* Store a function in a variable&lt;br /&gt;
* Pass a function into another function as an argument&lt;br /&gt;
* Get a function back as a return value&lt;br /&gt;
&lt;br /&gt;
This is what makes &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; &lt;br /&gt;
work. When you write &amp;lt;code&amp;gt;map(calc_weight, masses)&amp;lt;/code&amp;gt;, you are passing the &lt;br /&gt;
function &amp;lt;code&amp;gt;calc_weight&amp;lt;/code&amp;gt; itself into &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;. Then &lt;br /&gt;
&amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; runs it on every item in the list so you don&#039;t have to.&lt;br /&gt;
&lt;br /&gt;
There are a few reasons why writing code this way is efficient:&lt;br /&gt;
* &#039;&#039;&#039;It is easy to read:&#039;&#039;&#039; You can look at the code and immediately tell what it &lt;br /&gt;
is doing — like if you see &amp;quot;apply this formula to every mass&amp;quot; you just get it, &lt;br /&gt;
no decoding required&lt;br /&gt;
* &#039;&#039;&#039;Fewer bugs:&#039;&#039;&#039; Every time you write a for-loop you have to manually keep track of index &lt;br /&gt;
variables, which is easy to make mistakes on. These functions &lt;br /&gt;
handle the indices, so there is less room for errors.&lt;br /&gt;
* &#039;&#039;&#039;You can chain them:&#039;&#039;&#039; The output of &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; can go straight into &lt;br /&gt;
&amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; without extra steps, which keeps &lt;br /&gt;
the variables clean and short.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Lambda Expressions&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
A &#039;&#039;&#039;lambda expression&#039;&#039;&#039; is a function you write in one line without giving it a name. &lt;br /&gt;
The format is:&lt;br /&gt;
&lt;br /&gt;
 lambda &amp;lt;parameters&amp;gt;: &amp;lt;expression&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function only exists at the point where it is used. These two do the exact same thing:&lt;br /&gt;
&lt;br /&gt;
 # Regular way&lt;br /&gt;
 def square(x):&lt;br /&gt;
     return x**2&lt;br /&gt;
 &lt;br /&gt;
 # Lambda way&lt;br /&gt;
 square = lambda x: x**2&lt;br /&gt;
&lt;br /&gt;
Lambdas save you from having to define a whole separate function when you only &lt;br /&gt;
need it once:&lt;br /&gt;
&lt;br /&gt;
 # Without lambda: needs a function defined somewhere else&lt;br /&gt;
 result = list(map(square, [1, 2, 3, 4]))&lt;br /&gt;
 &lt;br /&gt;
 # With lambda: no separate function needed&lt;br /&gt;
 result = list(map(lambda x: x**2, [1, 2, 3, 4]))&lt;br /&gt;
 # Result: [1, 4, 9, 16]&lt;br /&gt;
&lt;br /&gt;
They can also take two inputs, which comes up when using &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 total = reduce(lambda acc, x: acc + x, [1, 2, 3, 4], 0)&lt;br /&gt;
 # Result: 10&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lambda vs def — when to use which:&#039;&#039;&#039;&lt;br /&gt;
* Use a lambda when the function is short, only needed once, and passed right &lt;br /&gt;
into another function&lt;br /&gt;
* Use &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when the function is longer or used in multiple places&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; Lambdas do not work in GlowScript at all. Always use &lt;br /&gt;
&amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when writing code for Trinket.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Inputs and Type Matching&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
All three functions follow the same basic structure:&lt;br /&gt;
&lt;br /&gt;
 function_name(function, list)&lt;br /&gt;
&lt;br /&gt;
The first argument is the function you want to use. The second is the list of &lt;br /&gt;
data you want to run it on, which can be a list, tuple, etc.&lt;br /&gt;
&lt;br /&gt;
One thing to watch out for: the function has to be able to work with any item &lt;br /&gt;
is in the list. If your list has decimal numbers but your function expects &lt;br /&gt;
whole numbers, Python will throw an error.&lt;br /&gt;
&lt;br /&gt;
Example using a named function:&lt;br /&gt;
 def cubed(x):&lt;br /&gt;
     return x**3&lt;br /&gt;
 &lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(cubed, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
The same thing with a lambda:&lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(lambda x: x**3, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
You can also pass Python&#039;s built-in functions straight in:&lt;br /&gt;
 words = [&#039;hello&#039;, &#039;world&#039;, &#039;vpython&#039;]&lt;br /&gt;
 lengths = list(map(len, words))&lt;br /&gt;
 # Result: [5, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Map()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;map(function, list)&amp;lt;/code&amp;gt; runs a function on every single element in a list &lt;br /&gt;
and gives you back all the results. The original list stays the same; you get a &lt;br /&gt;
new list of updated values.&lt;br /&gt;
&lt;br /&gt;
 map(function, list)&lt;br /&gt;
&lt;br /&gt;
So for a list &amp;lt;code&amp;gt;[a, b, c, d]&amp;lt;/code&amp;gt; and some function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;:&lt;br /&gt;
 map(f, [a, b, c, d])  →  [f(a), f(b), f(c), f(d)]&lt;br /&gt;
&lt;br /&gt;
=== Basic example ===&lt;br /&gt;
&lt;br /&gt;
 numlist = [1, 2, 3, 4, 5]&lt;br /&gt;
 result = list(map(lambda x: x * 2, numlist))&lt;br /&gt;
 # Result: [2, 4, 6, 8, 10]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational weight (F = mg) for the masses:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8  # m/s^2&lt;br /&gt;
 masses = [0.5, 1.0, 2.5, 5.0, 10.0]  # kg&lt;br /&gt;
 &lt;br /&gt;
 weights = list(map(lambda m: m * g, masses))&lt;br /&gt;
 # Result: [4.9, 9.8, 24.5, 49.0, 98.0]  Newtons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Kinetic energy of the velocities:&#039;&#039;&#039;&lt;br /&gt;
 mass = 2.0   # kg&lt;br /&gt;
 velocities = [3.0, 5.5, 2.1, 8.0]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 ke_list = list(map(lambda v: 0.5 * mass * v**2, velocities))&lt;br /&gt;
 # Result: [9.0, 30.25, 4.41, 64.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Converting temperature from Celsius to Kelvin:&#039;&#039;&#039;&lt;br /&gt;
 temps_C = [0, 20, 37, 100, -273.15]&lt;br /&gt;
 temps_K = list(map(lambda T: T + 273.15, temps_C))&lt;br /&gt;
 # Result: [273.15, 293.15, 310.15, 373.15, 0.0]  Kelvin&lt;br /&gt;
&lt;br /&gt;
=== map() vs a for-loop ===&lt;br /&gt;
&lt;br /&gt;
Both of these give the same answer, but &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; is more concise:&lt;br /&gt;
&lt;br /&gt;
 # For-loop&lt;br /&gt;
 weights = []&lt;br /&gt;
 for m in masses:&lt;br /&gt;
     weights.append(m * 9.8)&lt;br /&gt;
 &lt;br /&gt;
 # map() — same thing in one line&lt;br /&gt;
 weights = list(map(lambda m: m * 9.8, masses))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Filter()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;filter(function, list)&amp;lt;/code&amp;gt; iterates through a list and only keeps the elements &lt;br /&gt;
where the function returns &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 filter(function, list)&lt;br /&gt;
 filter(lambda x: condition, list)&lt;br /&gt;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
 numbers = [3, 7, 5, 2, 1, 6]&lt;br /&gt;
 result = list(filter(lambda x: x &amp;gt; 3, numbers))&lt;br /&gt;
 # Result: [7, 5, 6]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Retrieving only the fastest particles:&#039;&#039;&#039;&lt;br /&gt;
 speeds = [120, 340, 95, 500, 210, 80]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 fast_particles = list(filter(lambda v: v &amp;gt; 200, speeds))&lt;br /&gt;
 # Result: [340, 500, 210]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Keeping the positive charges:&#039;&#039;&#039;&lt;br /&gt;
 charges = [-1.6e-19, 1.6e-19, -3.2e-19, 3.2e-19, 0, 1.6e-19]  # Coulombs&lt;br /&gt;
 &lt;br /&gt;
 positive = list(filter(lambda q: q &amp;gt; 0, charges))&lt;br /&gt;
 # Result: [1.6e-19, 3.2e-19, 1.6e-19]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Removing particles that exited the simulation boundary:&#039;&#039;&#039;&lt;br /&gt;
 # Each particle p has a .pos.x value for its x position&lt;br /&gt;
 boundary = 10.0  # meters&lt;br /&gt;
 &lt;br /&gt;
 inside = list(filter(lambda p: abs(p.pos.x) &amp;lt; boundary, particles))&lt;br /&gt;
&lt;br /&gt;
=== Passing None as the function ===&lt;br /&gt;
&lt;br /&gt;
If you pass &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; instead of a function, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; removes &lt;br /&gt;
every zero, empty string, &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt; from the list:&lt;br /&gt;
&lt;br /&gt;
 messy = [1, 0, 3, None, 5, 0, 7]&lt;br /&gt;
 clean = list(filter(None, messy))&lt;br /&gt;
 # Result: [1, 3, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;reduce(function, list, starting value)&amp;lt;/code&amp;gt; takes a whole list and returns one single value. Unlike &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; &lt;br /&gt;
and &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, you do not get a list back — you get one number.&lt;br /&gt;
&lt;br /&gt;
The function you pass in needs to take two inputs:&lt;br /&gt;
* The running total so far&lt;br /&gt;
* The next item in the list&lt;br /&gt;
&lt;br /&gt;
After each step, the result becomes the new total for the next step.&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 reduce(function, list, starting value)&lt;br /&gt;
&lt;br /&gt;
=== Step-by-step walkthrough ===&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 numbers = [1, 2, 3, 4]&lt;br /&gt;
 result = reduce(lambda x, y: x * y, numbers)&lt;br /&gt;
 # Step 1: x=1, y=2  →  1 * 2 = 2&lt;br /&gt;
 # Step 2: x=2, y=3  →  2 * 3 = 6&lt;br /&gt;
 # Step 3: x=6, y=4  →  6 * 4 = 24&lt;br /&gt;
 # Final result: 24&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Adding up all the masses in a system:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 masses = [1.0, 2.0, 3.0, 4.0]  # kg&lt;br /&gt;
 total_mass = reduce(lambda acc, m: acc + m, masses, 0.0)&lt;br /&gt;
 # Result: 10.0 kg&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Finding the fastest particle in a list:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 speeds = [3.2, 7.8, 1.1, 9.4, 5.5]  # m/s&lt;br /&gt;
 max_speed = reduce(lambda a, b: a if a &amp;gt; b else b, speeds)&lt;br /&gt;
 # Result: 9.4 m/s&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Total work done (W = F·d):&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 forces = [10.0, 25.0, 5.0, 40.0]       # Newtons&lt;br /&gt;
 displacements = [2.0, 1.5, 3.0, 0.5]   # meters&lt;br /&gt;
 &lt;br /&gt;
 work_list = list(map(lambda fd: fd[0] * fd[1], zip(forces, displacements)))&lt;br /&gt;
 total_work = reduce(lambda acc, w: acc + w, work_list, 0.0)&lt;br /&gt;
 # Result: 20.0 + 37.5 + 15.0 + 20.0 = 92.5 Joules&lt;br /&gt;
&lt;br /&gt;
=== Watch out ===&lt;br /&gt;
&lt;br /&gt;
Always give &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; a starting value as the third argument. If the &lt;br /&gt;
list is empty and there is no starting value, Python will crash. With a starting &lt;br /&gt;
value, an empty list will give you that value back instead:&lt;br /&gt;
&lt;br /&gt;
 reduce(lambda acc, x: acc + x, [], 0.0)&lt;br /&gt;
 # Returns 0.0 safely&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Combining map(), filter(), and reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
It is also efficient that functions can be chained together.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example: kinetic energy&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 # Step 1: filter() — drop all values below 3.0 m/s&lt;br /&gt;
 moving = list(filter(lambda p: p.speed &amp;gt; 3.0, particles))&lt;br /&gt;
 &lt;br /&gt;
 # Step 2: map() — calculate KE for each remaining particle&lt;br /&gt;
 ke_list = list(map(lambda p: 0.5 * p.mass * p.speed**2, moving))&lt;br /&gt;
 &lt;br /&gt;
 # Step 3: reduce() — add them all up&lt;br /&gt;
 total_ke = reduce(lambda acc, ke: acc + ke, ke_list, 0.0)&lt;br /&gt;
 print(&amp;quot;Total KE of fast particles:&amp;quot;, round(total_ke, 2), &amp;quot;J&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
This filter → map → reduce pattern shows up all the time in physics simulations. &lt;br /&gt;
Pick a group of objects, apply a formula to each one, then get one final number &lt;br /&gt;
out of it. That is basically what these three functions are built for.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational potential energy at different heights:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8   # m/s^2&lt;br /&gt;
 m = 2.0   # kg&lt;br /&gt;
 heights = [1.0, 5.0, 10.0, 20.0, 50.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 pe_list = list(map(lambda h: m * g * h, heights))&lt;br /&gt;
 # Result: [19.6, 98.0, 196.0, 392.0, 980.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Electric force on an electron at different distances from a charge (Coulomb&#039;s law):&#039;&#039;&#039;&lt;br /&gt;
 k = 8.99e9   # N·m^2/C^2&lt;br /&gt;
 Q = 1.0e-6   # source charge, Coulombs&lt;br /&gt;
 r_list = [0.1, 0.2, 0.5, 1.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 forces = list(map(lambda r: k * Q * 1.6e-19 / r**2, r_list))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Interactive Simulation&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
The following GlowScript simulation shows all three functions working together &lt;br /&gt;
in a real physics example:&lt;br /&gt;
[https://trinket.io/glowscript/9e53d57b352f map(), filter(), and reduce() in VPython Physics — Trinket]&lt;br /&gt;
&lt;br /&gt;
The simulation puts five spheres in a row, each with a different mass between &lt;br /&gt;
1 and 8 kg and a different speed between 1.5 and 6 m/s. The size of each sphere &lt;br /&gt;
matches its mass so you can see the difference right away.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;map()&#039;&#039;&#039; goes through every mass and calculates the weight using F = mg. &lt;br /&gt;
The weight of each sphere gets printed to the console.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;filter()&#039;&#039;&#039; checks each sphere&#039;s speed and keeps the ones faster &lt;br /&gt;
than 3.0 m/s. &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;reduce()&#039;&#039;&#039; is written by hand since GlowScript does not have the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. It adds up the kinetic energy of every sphere &lt;br /&gt;
one by one until it has one total number for the whole system, which then &lt;br /&gt;
gets printed.&lt;br /&gt;
&lt;br /&gt;
This simulation is an accurate visualization of how all three functions work together.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;References&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
[http://www.bogotobogo.com/python/python_fncs_map_filter_reduce.php 1. Python map, filter, reduce — bogotobogo.com]&lt;br /&gt;
&lt;br /&gt;
[http://book.pythontips.com/en/latest/map_filter.html 2. Map, Filter — Python Tips]&lt;br /&gt;
&lt;br /&gt;
[http://vpython.org/contents/docs/VisualIntro.html 3. VPython Documentation — vpython.org]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functions.html 4. Python 3 Built-in Functions (map, filter) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functools.html 5. functools module (reduce) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/howto/functional.html 6. Functional Programming HOWTO — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-map-function/ 7. Python&#039;s map() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-filter-function/ 8. Python&#039;s filter() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-reduce-function/ 9. Python&#039;s reduce() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-lambda/ 10. Lambda Expressions in Python — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/functional-programming-in-python/ 11. Functional Programming in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/higher-order-functions-in-python/ 12. Higher-Order Functions in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.glowscript.org 13. GlowScript VPython — Official Site]&lt;br /&gt;
&lt;br /&gt;
[https://trinket.io/glowscript 14. GlowScript on Trinket]&lt;br /&gt;
&lt;br /&gt;
[https://vpython.org 15. VPython Official Documentation]&lt;/div&gt;</summary>
		<author><name>Sabrinayang</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48079</id>
		<title>VPython MapReduceFilter</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48079"/>
		<updated>2026-04-27T00:24:34Z</updated>

		<summary type="html">&lt;p&gt;Sabrinayang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;&amp;lt;big&amp;gt;Sabrina Yang - Spring 2026&amp;lt;/big&amp;gt;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Introduction&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; are &lt;br /&gt;
known as higher-order functions. Higher-order functions are functions that &lt;br /&gt;
take other functions as arguments. A typical VPython simulation can have hundreds of &lt;br /&gt;
objects that all follow the same physics equations, and writing for-loops to do the same &lt;br /&gt;
calculation over and over can be inefficient. &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &lt;br /&gt;
&amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; take care of the for-loops, &lt;br /&gt;
allowing you to focus on the point of the code itself.&lt;br /&gt;
&lt;br /&gt;
Each function does something different:&lt;br /&gt;
* &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;: applies a function to every element in a list&lt;br /&gt;
* &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;: iterates through a list and only keeps the elements that meet a certain condition&lt;br /&gt;
* &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;: takes a whole list and turns it into one single value&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Python 3 note:&#039;&#039;&#039; &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; do not actually work until you ask for the results. Wrap them in &amp;lt;code&amp;gt;list()&amp;lt;/code&amp;gt; to get &lt;br /&gt;
a list back. &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; has to be imported before you can use it:&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; GlowScript does not support lambda expressions or the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. Use regular &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; functions instead of &lt;br /&gt;
lambdas, and write &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; yourself. Here is an example of this &lt;br /&gt;
in the simulation section below.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Background&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, functions are objects just like numbers or strings. That means you can:&lt;br /&gt;
* Store a function in a variable&lt;br /&gt;
* Pass a function into another function as an argument&lt;br /&gt;
* Get a function back as a return value&lt;br /&gt;
&lt;br /&gt;
This is what makes &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; &lt;br /&gt;
work. When you write &amp;lt;code&amp;gt;map(calc_weight, masses)&amp;lt;/code&amp;gt;, you are passing the &lt;br /&gt;
function &amp;lt;code&amp;gt;calc_weight&amp;lt;/code&amp;gt; itself into &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;. Then &lt;br /&gt;
&amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; runs it on every item in the list so you don&#039;t have to.&lt;br /&gt;
&lt;br /&gt;
There are a few reasons why writing code this way is efficient:&lt;br /&gt;
* &#039;&#039;&#039;It is easy to read:&#039;&#039;&#039; You can look at the code and immediately tell what it &lt;br /&gt;
is doing — like if you see &amp;quot;apply this formula to every mass&amp;quot; you just get it, &lt;br /&gt;
no decoding required&lt;br /&gt;
* &#039;&#039;&#039;Fewer bugs:&#039;&#039;&#039; Every time you write a for-loop you have to manually keep track of index &lt;br /&gt;
variables, which is easy to make mistakes on. These functions &lt;br /&gt;
handle the indices, so there is less room for errors.&lt;br /&gt;
* &#039;&#039;&#039;You can chain them:&#039;&#039;&#039; The output of &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; can go straight into &lt;br /&gt;
&amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; without extra steps, which keeps &lt;br /&gt;
the variables clean and short.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Lambda Expressions&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
A &#039;&#039;&#039;lambda expression&#039;&#039;&#039; is a function you write in one line without giving it a name. &lt;br /&gt;
The format is:&lt;br /&gt;
&lt;br /&gt;
 lambda &amp;lt;parameters&amp;gt;: &amp;lt;expression&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function only exists at the point where it is used. These two do the exact same thing:&lt;br /&gt;
&lt;br /&gt;
 # Regular way&lt;br /&gt;
 def square(x):&lt;br /&gt;
     return x**2&lt;br /&gt;
 &lt;br /&gt;
 # Lambda way&lt;br /&gt;
 square = lambda x: x**2&lt;br /&gt;
&lt;br /&gt;
Lambdas save you from having to define a whole separate function when you only &lt;br /&gt;
need it once:&lt;br /&gt;
&lt;br /&gt;
 # Without lambda: needs a function defined somewhere else&lt;br /&gt;
 result = list(map(square, [1, 2, 3, 4]))&lt;br /&gt;
 &lt;br /&gt;
 # With lambda: no separate function needed&lt;br /&gt;
 result = list(map(lambda x: x**2, [1, 2, 3, 4]))&lt;br /&gt;
 # Result: [1, 4, 9, 16]&lt;br /&gt;
&lt;br /&gt;
They can also take two inputs, which comes up when using &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 total = reduce(lambda acc, x: acc + x, [1, 2, 3, 4], 0)&lt;br /&gt;
 # Result: 10&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lambda vs def — when to use which:&#039;&#039;&#039;&lt;br /&gt;
* Use a lambda when the function is short, only needed once, and passed right &lt;br /&gt;
into another function&lt;br /&gt;
* Use &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when the function is longer or used in multiple places&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript note:&#039;&#039;&#039; Lambdas do not work in GlowScript at all. Always use &lt;br /&gt;
&amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when writing code for Trinket.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Inputs and Type Matching&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
All three functions follow the same basic structure:&lt;br /&gt;
&lt;br /&gt;
 function_name(function, list)&lt;br /&gt;
&lt;br /&gt;
The first argument is the function you want to use. The second is the list of &lt;br /&gt;
data you want to run it on, which can be a list, tuple, etc.&lt;br /&gt;
&lt;br /&gt;
One thing to watch out for: the function has to be able to work with any item &lt;br /&gt;
is in the list. If your list has decimal numbers but your function expects &lt;br /&gt;
whole numbers, Python will throw an error.&lt;br /&gt;
&lt;br /&gt;
Example using a named function:&lt;br /&gt;
 def cubed(x):&lt;br /&gt;
     return x**3&lt;br /&gt;
 &lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(cubed, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
The same thing with a lambda:&lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(lambda x: x**3, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
You can also pass Python&#039;s built-in functions straight in:&lt;br /&gt;
 words = [&#039;hello&#039;, &#039;world&#039;, &#039;vpython&#039;]&lt;br /&gt;
 lengths = list(map(len, words))&lt;br /&gt;
 # Result: [5, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Map()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;map(function, list)&amp;lt;/code&amp;gt; runs a function on every single element in a list &lt;br /&gt;
and gives you back all the results. The original list stays the same; you get a &lt;br /&gt;
new list of updated values.&lt;br /&gt;
&lt;br /&gt;
 map(function, list)&lt;br /&gt;
&lt;br /&gt;
So for a list &amp;lt;code&amp;gt;[a, b, c, d]&amp;lt;/code&amp;gt; and some function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;:&lt;br /&gt;
 map(f, [a, b, c, d])  →  [f(a), f(b), f(c), f(d)]&lt;br /&gt;
&lt;br /&gt;
=== Basic example ===&lt;br /&gt;
&lt;br /&gt;
 numlist = [1, 2, 3, 4, 5]&lt;br /&gt;
 result = list(map(lambda x: x * 2, numlist))&lt;br /&gt;
 # Result: [2, 4, 6, 8, 10]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational weight (F = mg) for the masses:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8  # m/s^2&lt;br /&gt;
 masses = [0.5, 1.0, 2.5, 5.0, 10.0]  # kg&lt;br /&gt;
 &lt;br /&gt;
 weights = list(map(lambda m: m * g, masses))&lt;br /&gt;
 # Result: [4.9, 9.8, 24.5, 49.0, 98.0]  Newtons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Kinetic energy of the velocities:&#039;&#039;&#039;&lt;br /&gt;
 mass = 2.0   # kg&lt;br /&gt;
 velocities = [3.0, 5.5, 2.1, 8.0]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 ke_list = list(map(lambda v: 0.5 * mass * v**2, velocities))&lt;br /&gt;
 # Result: [9.0, 30.25, 4.41, 64.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Converting temperature from Celsius to Kelvin:&#039;&#039;&#039;&lt;br /&gt;
 temps_C = [0, 20, 37, 100, -273.15]&lt;br /&gt;
 temps_K = list(map(lambda T: T + 273.15, temps_C))&lt;br /&gt;
 # Result: [273.15, 293.15, 310.15, 373.15, 0.0]  Kelvin&lt;br /&gt;
&lt;br /&gt;
=== map() vs a for-loop ===&lt;br /&gt;
&lt;br /&gt;
Both of these give the same answer, but &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; is more concise:&lt;br /&gt;
&lt;br /&gt;
 # For-loop&lt;br /&gt;
 weights = []&lt;br /&gt;
 for m in masses:&lt;br /&gt;
     weights.append(m * 9.8)&lt;br /&gt;
 &lt;br /&gt;
 # map() — same thing in one line&lt;br /&gt;
 weights = list(map(lambda m: m * 9.8, masses))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Filter()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;filter(function, list)&amp;lt;/code&amp;gt; iterates through a list and only keeps the elements &lt;br /&gt;
where the function returns &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
 filter(function, list)&lt;br /&gt;
 filter(lambda x: condition, list)&lt;br /&gt;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
&lt;br /&gt;
 numbers = [3, 7, 5, 2, 1, 6]&lt;br /&gt;
 result = list(filter(lambda x: x &amp;gt; 3, numbers))&lt;br /&gt;
 # Result: [7, 5, 6]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Retrieving only the fastest particles:&#039;&#039;&#039;&lt;br /&gt;
 speeds = [120, 340, 95, 500, 210, 80]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 fast_particles = list(filter(lambda v: v &amp;gt; 200, speeds))&lt;br /&gt;
 # Result: [340, 500, 210]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Keeping the positive charges:&#039;&#039;&#039;&lt;br /&gt;
 charges = [-1.6e-19, 1.6e-19, -3.2e-19, 3.2e-19, 0, 1.6e-19]  # Coulombs&lt;br /&gt;
 &lt;br /&gt;
 positive = list(filter(lambda q: q &amp;gt; 0, charges))&lt;br /&gt;
 # Result: [1.6e-19, 3.2e-19, 1.6e-19]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Removing particles that exited the simulation boundary:&#039;&#039;&#039;&lt;br /&gt;
 # Each particle p has a .pos.x value for its x position&lt;br /&gt;
 boundary = 10.0  # meters&lt;br /&gt;
 &lt;br /&gt;
 inside = list(filter(lambda p: abs(p.pos.x) &amp;lt; boundary, particles))&lt;br /&gt;
&lt;br /&gt;
=== Passing None as the function ===&lt;br /&gt;
&lt;br /&gt;
If you pass &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; instead of a function, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; removes &lt;br /&gt;
every zero, empty string, &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt; from the list:&lt;br /&gt;
&lt;br /&gt;
 messy = [1, 0, 3, None, 5, 0, 7]&lt;br /&gt;
 clean = list(filter(None, messy))&lt;br /&gt;
 # Result: [1, 3, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;reduce(function, list, starting value)&amp;lt;/code&amp;gt; takes a whole list and returns one single value. Unlike &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; &lt;br /&gt;
and &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, you do not get a list back — you get one number.&lt;br /&gt;
&lt;br /&gt;
The function you pass in needs to take two inputs:&lt;br /&gt;
* The running total so far&lt;br /&gt;
* The next item in the list&lt;br /&gt;
&lt;br /&gt;
After each step, the result becomes the new total for the next step.&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 reduce(function, list, starting value)&lt;br /&gt;
&lt;br /&gt;
=== Step-by-step walkthrough ===&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 numbers = [1, 2, 3, 4]&lt;br /&gt;
 result = reduce(lambda x, y: x * y, numbers)&lt;br /&gt;
 # Step 1: x=1, y=2  →  1 * 2 = 2&lt;br /&gt;
 # Step 2: x=2, y=3  →  2 * 3 = 6&lt;br /&gt;
 # Step 3: x=6, y=4  →  6 * 4 = 24&lt;br /&gt;
 # Final result: 24&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Adding up all the masses in a system:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 masses = [1.0, 2.0, 3.0, 4.0]  # kg&lt;br /&gt;
 total_mass = reduce(lambda acc, m: acc + m, masses, 0.0)&lt;br /&gt;
 # Result: 10.0 kg&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Finding the fastest particle in a list:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 speeds = [3.2, 7.8, 1.1, 9.4, 5.5]  # m/s&lt;br /&gt;
 max_speed = reduce(lambda a, b: a if a &amp;gt; b else b, speeds)&lt;br /&gt;
 # Result: 9.4 m/s&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Total work done (W = F·d):&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 forces = [10.0, 25.0, 5.0, 40.0]       # Newtons&lt;br /&gt;
 displacements = [2.0, 1.5, 3.0, 0.5]   # meters&lt;br /&gt;
 &lt;br /&gt;
 work_list = list(map(lambda fd: fd[0] * fd[1], zip(forces, displacements)))&lt;br /&gt;
 total_work = reduce(lambda acc, w: acc + w, work_list, 0.0)&lt;br /&gt;
 # Result: 20.0 + 37.5 + 15.0 + 20.0 = 92.5 Joules&lt;br /&gt;
&lt;br /&gt;
=== Watch out ===&lt;br /&gt;
&lt;br /&gt;
Always give &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; a starting value as the third argument. If the &lt;br /&gt;
list is empty and there is no starting value, Python will crash. With a starting &lt;br /&gt;
value, an empty list will give you that value back instead:&lt;br /&gt;
&lt;br /&gt;
 reduce(lambda acc, x: acc + x, [], 0.0)&lt;br /&gt;
 # Returns 0.0 safely&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Combining map(), filter(), and reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
It is also efficient that functions can be chained together.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example: kinetic energy&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 # Step 1: filter() — drop all values below 3.0 m/s&lt;br /&gt;
 moving = list(filter(lambda p: p.speed &amp;gt; 3.0, particles))&lt;br /&gt;
 &lt;br /&gt;
 # Step 2: map() — calculate KE for each remaining particle&lt;br /&gt;
 ke_list = list(map(lambda p: 0.5 * p.mass * p.speed**2, moving))&lt;br /&gt;
 &lt;br /&gt;
 # Step 3: reduce() — add them all up&lt;br /&gt;
 total_ke = reduce(lambda acc, ke: acc + ke, ke_list, 0.0)&lt;br /&gt;
 print(&amp;quot;Total KE of fast particles:&amp;quot;, round(total_ke, 2), &amp;quot;J&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
This filter → map → reduce pattern shows up all the time in physics simulations. &lt;br /&gt;
Pick a group of objects, apply a formula to each one, then get one final number &lt;br /&gt;
out of it. That is basically what these three functions are built for.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Gravitational potential energy at different heights:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8   # m/s^2&lt;br /&gt;
 m = 2.0   # kg&lt;br /&gt;
 heights = [1.0, 5.0, 10.0, 20.0, 50.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 pe_list = list(map(lambda h: m * g * h, heights))&lt;br /&gt;
 # Result: [19.6, 98.0, 196.0, 392.0, 980.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Electric force on an electron at different distances from a charge (Coulomb&#039;s law):&#039;&#039;&#039;&lt;br /&gt;
 k = 8.99e9   # N·m^2/C^2&lt;br /&gt;
 Q = 1.0e-6   # source charge, Coulombs&lt;br /&gt;
 r_list = [0.1, 0.2, 0.5, 1.0]  # meters&lt;br /&gt;
 &lt;br /&gt;
 forces = list(map(lambda r: k * Q * 1.6e-19 / r**2, r_list))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Interactive Simulation&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
The following GlowScript simulation shows all three functions working together &lt;br /&gt;
in a real physics example:&lt;br /&gt;
[https://trinket.io/glowscript/9e53d57b352f map(), filter(), and reduce() in VPython Physics — Trinket]&lt;br /&gt;
&lt;br /&gt;
The simulation puts five spheres in a row, each with a different mass between &lt;br /&gt;
1 and 8 kg and a different speed between 1.5 and 6 m/s. The size of each sphere &lt;br /&gt;
matches its mass so you can see the difference right away.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;map()&#039;&#039;&#039; goes through every mass and calculates the weight using F = mg. &lt;br /&gt;
The weight of each sphere gets printed to the console.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;filter()&#039;&#039;&#039; checks each sphere&#039;s speed and keeps the ones faster &lt;br /&gt;
than 3.0 m/s. &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;reduce()&#039;&#039;&#039; is written by hand since GlowScript does not have the &lt;br /&gt;
&amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. It adds up the kinetic energy of every sphere &lt;br /&gt;
one by one until it has one total number for the whole system, which then &lt;br /&gt;
gets printed.&lt;br /&gt;
&lt;br /&gt;
This simulation is an accurate visualization of how all three functions work together.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;References&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
[http://www.bogotobogo.com/python/python_fncs_map_filter_reduce.php 1. Python map, filter, reduce — bogotobogo.com]&lt;br /&gt;
&lt;br /&gt;
[http://book.pythontips.com/en/latest/map_filter.html 2. Map, Filter — Python Tips]&lt;br /&gt;
&lt;br /&gt;
[http://vpython.org/contents/docs/VisualIntro.html 3. VPython Documentation — vpython.org]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functions.html 4. Python 3 Built-in Functions (map, filter) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functools.html 5. functools module (reduce) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/howto/functional.html 6. Functional Programming HOWTO — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-map-function/ 7. Python&#039;s map() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-filter-function/ 8. Python&#039;s filter() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-reduce-function/ 9. Python&#039;s reduce() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-lambda/ 10. Lambda Expressions in Python — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/functional-programming-in-python/ 11. Functional Programming in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/higher-order-functions-in-python/ 12. Higher-Order Functions in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.glowscript.org 13. GlowScript VPython — Official Site]&lt;br /&gt;
&lt;br /&gt;
[https://trinket.io/glowscript 14. GlowScript on Trinket]&lt;br /&gt;
&lt;br /&gt;
[https://vpython.org 15. VPython Official Documentation]&lt;/div&gt;</summary>
		<author><name>Sabrinayang</name></author>
	</entry>
	<entry>
		<id>http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48065</id>
		<title>VPython MapReduceFilter</title>
		<link rel="alternate" type="text/html" href="http://www.physicsbook.gatech.edu/index.php?title=VPython_MapReduceFilter&amp;diff=48065"/>
		<updated>2026-04-26T23:13:03Z</updated>

		<summary type="html">&lt;p&gt;Sabrinayang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;&amp;lt;big&amp;gt;Sabrina Yang - Spring 2026&amp;lt;/big&amp;gt;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Introduction&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
In Python, &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; are &lt;br /&gt;
known as &#039;&#039;&#039;higher-order functions&#039;&#039;&#039;. Higher-order functions are functions that take other functions as arguments. &lt;br /&gt;
These functions are especially valuable. &lt;br /&gt;
A typical simulation can include hundreds of objects and variables that each operate under the same physics laws. Instead of writing &lt;br /&gt;
for loops to iterate through each object, &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, &lt;br /&gt;
and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; handles the iteration automatically, which makes the code shorter and more efficient.&lt;br /&gt;
&lt;br /&gt;
The three functions each serve a its own role:&lt;br /&gt;
* &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;: &#039;&#039;&#039;transforms&#039;&#039;&#039; every element in a list by applying a function to it&lt;br /&gt;
* &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;: &#039;&#039;&#039;selects&#039;&#039;&#039; a subset of elements that satisfy a condition&lt;br /&gt;
* &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;: &#039;&#039;&#039;aggregates&#039;&#039;&#039; all elements into a single accumulated value&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important note for Python 3:&#039;&#039;&#039; &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; return &lt;br /&gt;
lazy iterator objects, in other words objects that don&#039;t do any work until you ask it for the results, rather than lists. This means they do not compute anything until &lt;br /&gt;
you actually need the values. Wrap them in &amp;lt;code&amp;gt;list()&amp;lt;/code&amp;gt; to force evaluation and &lt;br /&gt;
get a plain list back. &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; was removed from Python 3&#039;s built-ins and &lt;br /&gt;
must be explicitly imported:&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important note for GlowScript VPython:&#039;&#039;&#039; GlowScript runs a restricted subset of &lt;br /&gt;
Python and does not support lambda expressions or the &amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. &lt;br /&gt;
Use named &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; functions instead of lambdas, and implement &lt;br /&gt;
&amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; manually as shown in the interactive simulation section below.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Background: Functional Programming&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
To fully understand &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;, &lt;br /&gt;
you need to understand first-class functions. In Python, all functions &lt;br /&gt;
are first class objects, meaning they can be:&lt;br /&gt;
* Assigned to variables&lt;br /&gt;
* Passed as arguments to other functions&lt;br /&gt;
* Returned as values from other functions&lt;br /&gt;
&lt;br /&gt;
This is what makes higher-order functions possible. When you write &lt;br /&gt;
&amp;lt;code&amp;gt;map(calc_weight, masses)&amp;lt;/code&amp;gt;, you are passing the function &lt;br /&gt;
&amp;lt;code&amp;gt;calc_weight&amp;lt;/code&amp;gt; itself as an argument to &lt;br /&gt;
&amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; then calls &amp;lt;code&amp;gt;calc_weight&amp;lt;/code&amp;gt; &lt;br /&gt;
for each element.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Lambda Expressions&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
A &#039;&#039;&#039;lambda expression&#039;&#039;&#039; is a function you write in one line without giving it a name. The syntax is:&lt;br /&gt;
&lt;br /&gt;
 lambda &amp;lt;parameters&amp;gt;: &amp;lt;expression&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function exists only at the point where &lt;br /&gt;
it is used. These two definitions produce the same results:&lt;br /&gt;
&lt;br /&gt;
 # Traditional way&lt;br /&gt;
 def square(x):&lt;br /&gt;
     return x**2&lt;br /&gt;
 &lt;br /&gt;
 # Using lambda&lt;br /&gt;
 square = lambda x: x**2&lt;br /&gt;
&lt;br /&gt;
Lambdas save you from having to define a whole separate function when you only need it once:&lt;br /&gt;
&lt;br /&gt;
 # Without lambda: requires a function defined somewhere else&lt;br /&gt;
 result = list(map(square, [1, 2, 3, 4]))&lt;br /&gt;
 &lt;br /&gt;
 # With lambda: no defined function needed&lt;br /&gt;
 result = list(map(lambda x: x**2, [1, 2, 3, 4]))&lt;br /&gt;
 # Result: [1, 4, 9, 16]&lt;br /&gt;
&lt;br /&gt;
Lambda expressions can also take multiple parameters, which is useful for &lt;br /&gt;
&amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 total = reduce(lambda acc, x: acc + x, [1, 2, 3, 4], 0)&lt;br /&gt;
 # Result: 10&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;When to use lambdas vs def:&#039;&#039;&#039;&lt;br /&gt;
* Use a lambda when the function is short, used only once, and &lt;br /&gt;
passed directly as an argument&lt;br /&gt;
* Use &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; when the function is longer than one line or reused constantly&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;GlowScript limitation:&#039;&#039;&#039; Lambda expressions are not supported in GlowScript VPython. &lt;br /&gt;
Always use named &amp;lt;code&amp;gt;def&amp;lt;/code&amp;gt; functions when writing code for Trinket or &lt;br /&gt;
GlowScript.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Inputs and Type Matching&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
All three functions share the same basic calling convention:&lt;br /&gt;
&lt;br /&gt;
 higher_order_function(function, iterable)&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;function&#039;&#039;&#039; argument describes the operation to perform. The &#039;&#039;&#039;iterable&#039;&#039;&#039; &lt;br /&gt;
argument is the sequence of data to operate on, which can be a list, tuple, range, &lt;br /&gt;
or any other iterable Python object.&lt;br /&gt;
&lt;br /&gt;
One important constraint is &#039;&#039;&#039;type matching&#039;&#039;&#039;: the function must be able to accept &lt;br /&gt;
the types contained in the iterable. For example, if your list contains floating-point &lt;br /&gt;
numbers, your function must expect floats. Mismatches will cause a &amp;lt;code&amp;gt;TypeError&amp;lt;/code&amp;gt; &lt;br /&gt;
at runtime.&lt;br /&gt;
&lt;br /&gt;
Example using a named function:&lt;br /&gt;
 def cubed(x):&lt;br /&gt;
     return x**3&lt;br /&gt;
 &lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(cubed, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
The same using a lambda:&lt;br /&gt;
 items = [1, 2, 3, 4]&lt;br /&gt;
 result = list(map(lambda x: x**3, items))&lt;br /&gt;
 # Result: [1, 8, 27, 64]&lt;br /&gt;
&lt;br /&gt;
You can also pass Python&#039;s built-in functions directly:&lt;br /&gt;
 words = [&#039;hello&#039;, &#039;world&#039;, &#039;vpython&#039;]&lt;br /&gt;
 lengths = list(map(len, words))&lt;br /&gt;
 # Result: [5, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Map()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;map(function, iterable)&amp;lt;/code&amp;gt; applies a function to &#039;&#039;&#039;every element&#039;&#039;&#039; of an &lt;br /&gt;
iterable and returns an iterator of the results. The original list is never modified. Instead,&lt;br /&gt;
a new sequence of transformed values is produced. The output always has the same number &lt;br /&gt;
of elements as the input.&lt;br /&gt;
&lt;br /&gt;
 map(function, iterable)&lt;br /&gt;
&lt;br /&gt;
For a list &amp;lt;code&amp;gt;[a, b, c, d]&amp;lt;/code&amp;gt; and a function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;:&lt;br /&gt;
 map(f, [a, b, c, d])  →  [f(a), f(b), f(c), f(d)]&lt;br /&gt;
&lt;br /&gt;
=== Basic example ===&lt;br /&gt;
&lt;br /&gt;
 numlist = [1, 2, 3, 4, 5]&lt;br /&gt;
 result = list(map(lambda x: x * 2, numlist))&lt;br /&gt;
 # Result: [2, 4, 6, 8, 10]&lt;br /&gt;
&lt;br /&gt;
=== Examples using Physics ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Computing gravitational weight (F = mg) for a set of masses:&#039;&#039;&#039;&lt;br /&gt;
 g = 9.8  # m/s^2&lt;br /&gt;
 masses = [0.5, 1.0, 2.5, 5.0, 10.0]  # kg&lt;br /&gt;
 &lt;br /&gt;
 weights = list(map(lambda m: m * g, masses))&lt;br /&gt;
 # Result: [4.9, 9.8, 24.5, 49.0, 98.0]  Newtons&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Computing kinetic energy (KE = ½mv²) for a set of velocities:&#039;&#039;&#039;&lt;br /&gt;
 mass = 2.0   # kg&lt;br /&gt;
 velocities = [3.0, 5.5, 2.1, 8.0]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 ke_list = list(map(lambda v: 0.5 * mass * v**2, velocities))&lt;br /&gt;
 # Result: [9.0, 30.25, 4.41, 64.0]  Joules&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Converting a list of temperatures from Celsius to Kelvin:&#039;&#039;&#039;&lt;br /&gt;
 temps_C = [0, 20, 37, 100, -273.15]&lt;br /&gt;
 temps_K = list(map(lambda T: T + 273.15, temps_C))&lt;br /&gt;
 # Result: [273.15, 293.15, 310.15, 373.15, 0.0]  Kelvin&lt;br /&gt;
&lt;br /&gt;
=== Why use map() instead of a for-loop? ===&lt;br /&gt;
&lt;br /&gt;
Both of the following produce the same result, but &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; is shorter, and expresses the point more directly:&lt;br /&gt;
&lt;br /&gt;
 # For-loop approach&lt;br /&gt;
 weights = []&lt;br /&gt;
 for m in masses:&lt;br /&gt;
     weights.append(m * 9.8)&lt;br /&gt;
 &lt;br /&gt;
 # map() approach — same result, fewer lines&lt;br /&gt;
 weights = list(map(lambda m: m * 9.8, masses))&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Filter()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;filter(function, iterable)&amp;lt;/code&amp;gt; keeps only the elements for which the function &lt;br /&gt;
returns &amp;lt;code&amp;gt;True&amp;lt;/code&amp;gt;. The function must return a boolean value (or a value Python &lt;br /&gt;
treats as truthy or falsy). The output list may be shorter than the input list.&lt;br /&gt;
&lt;br /&gt;
 filter(function, iterable)&lt;br /&gt;
 filter(lambda x: condition, iterable)&lt;br /&gt;
&lt;br /&gt;
Conceptually, for a list &amp;lt;code&amp;gt;[a, b, c, d]&amp;lt;/code&amp;gt; and a predicate function &lt;br /&gt;
&amp;lt;code&amp;gt;p&amp;lt;/code&amp;gt;:&lt;br /&gt;
 filter(p, [a, b, c, d])  →  [x for x in [a,b,c,d] if p(x) is True]&lt;br /&gt;
&lt;br /&gt;
=== Basic example ===&lt;br /&gt;
&lt;br /&gt;
 numbers = [3, 7, 5, 2, 1, 6]&lt;br /&gt;
 result = list(filter(lambda x: x &amp;gt; 3, numbers))&lt;br /&gt;
 # Result: [7, 5, 6]&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Isolating particles moving above a speed threshold:&#039;&#039;&#039;&lt;br /&gt;
 speeds = [120, 340, 95, 500, 210, 80]  # m/s&lt;br /&gt;
 &lt;br /&gt;
 fast_particles = list(filter(lambda v: v &amp;gt; 200, speeds))&lt;br /&gt;
 # Result: [340, 500, 210]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Keeping only positive charges from a mixed list:&#039;&#039;&#039;&lt;br /&gt;
 charges = [-1.6e-19, 1.6e-19, -3.2e-19, 3.2e-19, 0, 1.6e-19]  # Coulombs&lt;br /&gt;
 &lt;br /&gt;
 positive = list(filter(lambda q: q &amp;gt; 0, charges))&lt;br /&gt;
 # Result: [1.6e-19, 3.2e-19, 1.6e-19]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Filtering out particles that have left the simulation boundary:&#039;&#039;&#039;&lt;br /&gt;
 # Each particle p has a .pos.x attribute representing its x position&lt;br /&gt;
 boundary = 10.0  # meters&lt;br /&gt;
 &lt;br /&gt;
 inside = list(filter(lambda p: abs(p.pos.x) &amp;lt; boundary, particles))&lt;br /&gt;
&lt;br /&gt;
=== Passing None as the function ===&lt;br /&gt;
&lt;br /&gt;
A special case: passing &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; instead of a function removes all &lt;br /&gt;
false values from the list (zeros, empty strings, &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;False&amp;lt;/code&amp;gt;):&lt;br /&gt;
&lt;br /&gt;
 messy = [1, 0, 3, None, 5, 0, 7]&lt;br /&gt;
 clean = list(filter(None, messy))&lt;br /&gt;
 # Result: [1, 3, 5, 7]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
=== How it works ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;reduce(function, iterable, initializer)&amp;lt;/code&amp;gt; &#039;&#039;&#039;cumulatively&#039;&#039;&#039; applies a &lt;br /&gt;
two-argument function to the elements of a list, reducing the entire sequence down &lt;br /&gt;
to a single scalar value.&lt;br /&gt;
&lt;br /&gt;
The function passed to &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt; must accept exactly two arguments:&lt;br /&gt;
* The &#039;&#039;&#039;accumulator&#039;&#039;&#039; — the running result&lt;br /&gt;
* The &#039;&#039;&#039;current element&#039;&#039;&#039; — the next item from the list&lt;br /&gt;
&lt;br /&gt;
After each call, the return value becomes the new accumulator for the next call.&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 reduce(function, iterable, initializer)&lt;br /&gt;
&lt;br /&gt;
=== Step-by-step walkthrough ===&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 numbers = [1, 2, 3, 4]&lt;br /&gt;
 result = reduce(lambda x, y: x * y, numbers)&lt;br /&gt;
 # Step 1: x=1, y=2  →  1 * 2 = 2&lt;br /&gt;
 # Step 2: x=2, y=3  →  2 * 3 = 6&lt;br /&gt;
 # Step 3: x=6, y=4  →  6 * 4 = 24&lt;br /&gt;
 # Final result: 24&lt;br /&gt;
&lt;br /&gt;
=== Physics examples ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Adding all masses in a system:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 masses = [1.0, 2.0, 3.0, 4.0]  # kg&lt;br /&gt;
 total_mass = reduce(lambda acc, m: acc + m, masses, 0.0)&lt;br /&gt;
 # Result: 10.0 kg&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Finding the maximum speed in a list of particles:&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 speeds = [3.2, 7.8, 1.1, 9.4, 5.5]  # m/s&lt;br /&gt;
 max_speed = reduce(lambda a, b: a if a &amp;gt; b else b, speeds)&lt;br /&gt;
 # Result: 9.4 m/s&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Computing total work done by forces over different displacements (W = F·d):&#039;&#039;&#039;&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 forces = [10.0, 25.0, 5.0, 40.0]       # Newtons&lt;br /&gt;
 displacements = [2.0, 1.5, 3.0, 0.5]   # meters&lt;br /&gt;
 &lt;br /&gt;
 work_list = list(map(lambda fd: fd[0] * fd[1], zip(forces, displacements)))&lt;br /&gt;
 total_work = reduce(lambda acc, w: acc + w, work_list, 0.0)&lt;br /&gt;
 # Result: 20.0 + 37.5 + 15.0 + 20.0 = 92.5 Joules&lt;br /&gt;
&lt;br /&gt;
=== Important warning ===&lt;br /&gt;
&lt;br /&gt;
Always provide an initializer (third argument) when using &amp;lt;code&amp;gt;reduce()&amp;lt;/code&amp;gt;. &lt;br /&gt;
Calling it on an empty list without an initializer raises a &amp;lt;code&amp;gt;TypeError&amp;lt;/code&amp;gt;. &lt;br /&gt;
With an initializer, an empty list returns the initializer value:&lt;br /&gt;
&lt;br /&gt;
 reduce(lambda acc, x: acc + x, [], 0.0)&lt;br /&gt;
 # Returns 0.0 safely instead of raising an error&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Combining map(), filter(), and reduce()&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
The power of these functions come from chaining them together into a &lt;br /&gt;
data pipeline. Each function&#039;s output becomes the next function&#039;s input, creating &lt;br /&gt;
a clean sequence of transformation → selection → aggregation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Example — total kinetic energy of only the fast-moving particles in a system:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
 from functools import reduce&lt;br /&gt;
 &lt;br /&gt;
 # Step 1: filter() — only keep particles with a speed greater than 3 m/s&lt;br /&gt;
 moving = list(filter(lambda p: p.speed &amp;gt; 3.0, particles))&lt;br /&gt;
 &lt;br /&gt;
 # Step 2: map() — calculate the kinetic energy for each particle&lt;br /&gt;
 ke_list = list(map(lambda p: 0.5 * p.mass * p.speed**2, moving))&lt;br /&gt;
 &lt;br /&gt;
 # Step 3: reduce() — add all the kinetic energies&lt;br /&gt;
 total_ke = reduce(lambda acc, ke: acc + ke, ke_list, 0.0)&lt;br /&gt;
 print(&amp;quot;Total KE of fast particles:&amp;quot;, round(total_ke, 2), &amp;quot;J&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
This three-step pattern (filter, map, reduce) is one of the most widely used patterns in programming.&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;Interactive Simulation&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
The following GlowScript simulation demonstrates all three functions working &lt;br /&gt;
together in a physics context:&lt;br /&gt;
[https://trinket.io/glowscript/9e53d57b352f map(), filter(), and reduce() in VPython Physics — Trinket]&lt;br /&gt;
&lt;br /&gt;
The simulation creates five spheres arranged in a horizontal row, each assigned a &lt;br /&gt;
different mass (ranging from 1 to 8 kg) and a different speed (ranging from 1.5 to &lt;br /&gt;
6 m/s). The radius of each sphere is proportional to its mass.&lt;br /&gt;
&lt;br /&gt;
The program then demonstrates each function in sequence:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;map()&#039;&#039;&#039; iterates over the list of masses and computes the gravitational weight &lt;br /&gt;
of each sphere using Newton&#039;s second law (F = mg, with g = 9.8 m/s²). The results &lt;br /&gt;
are printed to the console, showing the weight in Newtons for each sphere.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;filter()&#039;&#039;&#039; checks every sphere&#039;s speed and keeps only those moving faster than &lt;br /&gt;
3.0 m/s. Those spheres are turned &#039;&#039;&#039;red&#039;&#039;&#039; in the 3D scene, providing a visual &lt;br /&gt;
indicator of which objects meet the threshold condition. The speeds of the selected &lt;br /&gt;
spheres are also printed.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;reduce()&#039;&#039;&#039; — implemented manually as a custom function since GlowScript does &lt;br /&gt;
not support the &amp;lt;code&amp;gt;functools&amp;lt;/code&amp;gt; module. It adds the kinetic energy of every sphere into a single total. The individual kinetic energies &lt;br /&gt;
and the system total are printed to the console.&lt;br /&gt;
&lt;br /&gt;
Together, the simulation illustrates the filter → map → reduce pipeline in a &lt;br /&gt;
physical setting, showing how the three functions complement each other.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== &#039;&#039;&#039;References&#039;&#039;&#039; ==&lt;br /&gt;
&lt;br /&gt;
[http://www.bogotobogo.com/python/python_fncs_map_filter_reduce.php 1. Python map, filter, reduce — bogotobogo.com]&lt;br /&gt;
&lt;br /&gt;
[http://book.pythontips.com/en/latest/map_filter.html 2. Map, Filter — Python Tips]&lt;br /&gt;
&lt;br /&gt;
[http://vpython.org/contents/docs/VisualIntro.html 3. VPython Documentation — vpython.org]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functions.html 4. Python 3 Built-in Functions (map, filter) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/library/functools.html 5. functools module (reduce) — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://docs.python.org/3/howto/functional.html 6. Functional Programming HOWTO — Python Software Foundation]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-map-function/ 7. Python&#039;s map() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-filter-function/ 8. Python&#039;s filter() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-reduce-function/ 9. Python&#039;s reduce() — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://realpython.com/python-lambda/ 10. Lambda Expressions in Python — Real Python]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/functional-programming-in-python/ 11. Functional Programming in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.geeksforgeeks.org/higher-order-functions-in-python/ 12. Higher-Order Functions in Python — GeeksforGeeks]&lt;br /&gt;
&lt;br /&gt;
[https://www.glowscript.org 13. GlowScript VPython — Official Site]&lt;br /&gt;
&lt;br /&gt;
[https://trinket.io/glowscript 14. GlowScript on Trinket]&lt;br /&gt;
&lt;br /&gt;
[https://vpython.org 15. VPython Official Documentation]&lt;/div&gt;</summary>
		<author><name>Sabrinayang</name></author>
	</entry>
</feed>