For Loops VS List Comprehension
During day twenty-six of Dr. Yu’s 100 day Python course she taught us about the similarities and differences between for loops and List Comprehensions, I wrote about for loops in an earlier blog that you can find here. For loops are highly useful in a multitude of situations, but they are clunky and require far too much code for users to be efficient with them. One way to integrate the power of a for loop into a faster, more efficient line of code is with list comprehension. While a for loop takes a minimum of four lines to impact a list of data a list comprehension can complete the same task in one.
For Loop:
numbers = [1, 2, 3]
new_list = []
for n in list:
add_1 = n + 1
new_list.append(add_1)
print(new_list)
#Result is [1, 2, 3]
List Comprehension:
numbers = [1, 2, 3]
new_list = [n + 1 for n in numbers]
print(new_list)
#Result is [1, 2, 3]
List Comprehension can even integrate an if statement into the loop so as to impact, move, and/or individualize only specific pieces of data. Some examples could be:
Squaring all numbers within a list:
numbers = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
squared_numbers = [num * num for num in numbers]
print(squared_numbers)
#Result is [1, 1, 4, 9, 25, 64, 169, 441, 1156, 3025]
Seeking Overlapping Data Between Two Lists (An Inner Join):
list1 = [3, 6, 5, 8, 33, 12, 7, 4, 72, 2, 42, 13]
list2 = [3, 6, 13, 5, 7, 89, 12, 3, 33, 34, 1, 344, 42]
overlap = [num for num in list1 if num in list2]
print(overlap)
#Result is [3, 6, 5, 33, 12, 7, 42, 13]
Dictionary comprehension is exactly the same as list comprehension, but with the added feature of preserving the relationship between keys and values. The only differences in syntax is the curly braces {} instead of the brackets [].
Changing Weather Record from Celsius to Fahrenheit:
weather_c = {"Monday": 12, "Tuesday": 14, "Wednesday": 15, "Thursday": 14, "Friday": 21, "Saturday": 22, "Sunday": 24}
weather_f = {day:temp * 9/5 + 32 for (day, temp) in weather_c}
print(weather_f)
#Result is {'Monday': 53.6, 'Tuesday': 57.2, 'Wednesday': 59.0, 'Thursday': 57.2, 'Friday': 69.8, 'Saturday': 71.6, 'Sunday': 75.2}
Justin Tadros is a Project Manager and Data Analyst at The Training Boss. Justin has a bachelor degree in Theater performance from Rollins College and currently pursuing his Masters in business at the University of Center Florida. Justin is certified on Microsoft Power BI and Progress Sitefinity Sales accreditation with on going training on Python and CMS technologies. Justin performs in theaters in Orlando, Boston, Alaska and stand up comic whenever the opportunity arises. His passion for performing and bringing incredible customer service to any industry he approaches is second to his commitment, dedication and hard work. |
Tags:
- AI (3)
- ASP.NET Core (3)
- Azure (13)
- Conference (2)
- Consulting (2)
- cookies (1)
- CreateStudio (5)
- creative (1)
- CRMs (4)
- Data Analytics (3)
- Databricks (1)
- Event (1)
- Fun (1)
- GenerativeAI (4)
- Github (1)
- Markup (1)
- Microsoft (13)
- Microsoft Fabric (2)
- NextJS (1)
- Proven Alliance (1)
- Python (6)
- Sales (5)
- Sitefinity (12)
- Snowflake (1)
- Social Networking (1)
- SQL (2)
- Teams (1)
- Training (2)
- Word Press (1)
- Znode (1)
Playlist for Sitefinity on YouTube
Playlist for Microsoft Fabric on YouTube
Playlist for AI on YouTube
Copyright © 2024 The Training Boss LLC
Developed with Sitefinity 15.1.8321 on ASP.NET 8