How To Write Efficient Python Code - Engineering Education (EngEd) Program - Section
How To Write Efficient Python Code - Engineering Education (EngEd) Program - Section
EngEd Community
Section’s Engineering Education (EngEd) Program fosters a community of university students in Computer
Science related fields of study to research and share topics that are relevant to engineers in the modern
technology landscape. You can find more information and program guidelines in the GitHub repository. If you're
currently enrolled in a Computer Science related field of study and are interested in participating in the
program, please complete this form
.
Prerequisites
List comprehension
The result would be the final list containing our data, the transform is
the value of each data in our list, it keeps on changing as the value of
the iterator changes. The iteration is a loop that helps populate our list
with the required amount of data. Finally, the filter (optional) is used to
filter out the data that we don’t want.
nums = []
for x in range(10):
nums.append(x)
print(nums)
https://www.section.io/engineering-education/how-to-write-efficient-python-code/ 2/13
8/16/22, 12:51 PM How to write efficient Python code | Engineering Education (EngEd) Program | Section
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
It took us about 3 lines of code. Let’s try to be more efficient using list
comprehension:
print(nums)
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Voilà, just one line of code to achieve the earlier result. It is way quicker
to write once you understand the syntax and flow of list comprehension.
print(nums)
Output:
https://www.section.io/engineering-education/how-to-write-efficient-python-code/ 3/13
8/16/22, 12:51 PM How to write efficient Python code | Engineering Education (EngEd) Program | Section
Another example:
print(nums)
Output:
With these two examples above, we added the ability to use the if/else
statement in our list comprehension. It should be noted that the elif
statement can’t be used within list comprehension.
Lambda functions
https://www.section.io/engineering-education/how-to-write-efficient-python-code/ 4/13
8/16/22, 12:51 PM How to write efficient Python code | Engineering Education (EngEd) Program | Section
The value at the left is referred to as the arguments, while the values at
the right are the expression. Let’s break the syntax further:
def even_or_odd(n):
if n % 2 == 0:
return "Even"
else:
return "Odd"
print(even_or_odd(10))
Output:
Even
This took us about five lines of code to write, with lambdas I’m happy to
say that it’ll only take one line:
https://www.section.io/engineering-education/how-to-write-efficient-python-code/ 5/13
8/16/22, 12:51 PM How to write efficient Python code | Engineering Education (EngEd) Program | Section
Output:
Odd
even_or_odd(8)
Output:
Even
When you are working with a list filled with data, map, filter, and reduce
give you the ability to perform common list manipulation tasks.
The first on the list is map. With the map function we can modify our list
however we want.
nums = [1, 2, 3, 4, 5]
print(squared_nums)
Output:
The map function takes two arguments, the function that would modify
the data, and the data itself. In our example, we used the lambda
function but we can use it without lambdas.
The map function coupled with the lambda function allows us to modify
our list with just one line of code.
Note that we used the list function to convert map objects back to a
list.
https://www.section.io/engineering-education/how-to-write-efficient-python-code/ 7/13
8/16/22, 12:51 PM How to write efficient Python code | Engineering Education (EngEd) Program | Section
The second one is filter, the filter function is used to filter out
unwanted data from our list.
print(even_nums)
Output:
[2, 4, 6, 8, 10]
The reduce function helps to reduce the items in our list to just one
value.
nums = [5 ,6 , 7 ,8 , 9, 10]
https://www.section.io/engineering-education/how-to-write-efficient-python-code/ 8/13
8/16/22, 12:51 PM How to write efficient Python code | Engineering Education (EngEd) Program | Section
print(sum_of_nums)
Output:
45
In our example, we used the reduce function to add up all the data in our
list.
Note that we have to import reduce from functools before we can use it.
The map, filter, and reduce save us time from creating our list
manipulation function. They are useful for many common cases.
Conclusion
I am glad that you have reached the end of this article, I encourage you
to use the concepts you have been introduced within your next Python
project and check out how it improves your efficiency as a developer.
Happy coding!
https://www.section.io/engineering-education/how-to-write-efficient-python-code/ 9/13
8/16/22, 12:51 PM How to write efficient Python code | Engineering Education (EngEd) Program | Section
Similar Articles
Languages, Node.js
Read More
https://www.section.io/engineering-education/how-to-write-efficient-python-code/ 10/13
8/16/22, 12:51 PM How to write efficient Python code | Engineering Education (EngEd) Program | Section
Languages
Read More
Languages
Read More
https://www.section.io/engineering-education/how-to-write-efficient-python-code/ 11/13
8/16/22, 12:51 PM How to write efficient Python code | Engineering Education (EngEd) Program | Section
Samuel Torimiro
Samuel Torimiro is enthusiastic about software engineering, teaching and entrepreneurship. His
goal is to build web and mobile applications to better serve hundreds of millions of customers.
Add to Slack
Company
About
Legals
Resources
Blog
Solution Briefs
Engineering Education
Pricing
https://www.section.io/engineering-education/how-to-write-efficient-python-code/ 12/13
8/16/22, 12:51 PM How to write efficient Python code | Engineering Education (EngEd) Program | Section
Support
Docs
Community Slack
Release Notes
Platform Status
Contact Us
cloud native
varnish cache the linux lf edge
computing foundation
logo foundation logo logo
logo
© 2022 Section
https://www.section.io/engineering-education/how-to-write-efficient-python-code/ 13/13