I was trying to speed up some nested for loops with multiple internal loops in python 3.11:
for item1 in iterable1:
for item2 in interable2:
if condition == true:
expression1
for item3 in interable3:
if condition2 == true:
expression2
Note that the 2nd and 3rd for loops are at the same level and not nested, unlike This Question.
I learned that list comprehension and the map() function is usually much faster than for loops Here, and I'm trying to convert my nested loops into map() function(s).
Can someone please explain how to construct such map() functions and/or list comprehensions?
I thought I would do some research - there would surely be a answer for such a problem on the internet, but left empty handed. some resources used include:
- Convert a nested for loop to a map equivalent
- List comprehension in Python, how to
- https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions & https://docs.python.org/3/tutorial/datastructures.html#nested-list-comprehensions
- https://www.programiz.com/python-programming/list-comprehension
- https://docs.python.org/3/tutorial/datastructures.html#looping-techniques
inputstatements with hard coded data? it's difficult to understand your question without a minimal, reproducible example.set('A', 'B')isn't valid Python. Also, if you're claiminig a need to speed things up, show benchmark values: you're iterating over a trivial number of items, why do you need to optimize this? What's the actual problem you're trying to solve? (e.g. what does your real code do that you benchmarked and profiled, and identified your loop constructions as being hot spots).