-2

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:

18
  • 2
    Can you please replace the input statements with hard coded data? it's difficult to understand your question without a minimal, reproducible example. Commented Jul 13, 2024 at 20:14
  • @LordElrond, okay, i'll do that right away! Commented Jul 13, 2024 at 20:21
  • "I learned that list comprehension and the map() function is usually much faster than for loops Here" - Um... that linked answer seems to say the exact opposite. Commented Jul 13, 2024 at 20:28
  • 2
    When showing a minimal reproducible example, verify it works, because 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). Commented Jul 13, 2024 at 20:38
  • 4
    Don't tell me, tell everyone by editing your post. If you're doing an online exercise, talk about that in your post. Please (re)read the posting guidelines and edit your post accordingly. If folks ask you questions, it's because your post left out details that you need to add in (by rewriting it to be a complete question. Don't use "edit: ...." sections, properly update the post text: it's going to be on here for years to come, and future visitors need to be able to understand whether your question from the past is their "current" question, too). Commented Jul 13, 2024 at 20:49

1 Answer 1

1

I think you're confusing several concepts:

First, List comprehensions and for loops are the same thing. A list comprehension is merely a more compact way to write a for loop. You can rewrite any list comprehension as a for loop with no meaningful change in performance.

For example, if you start with the following for loop:

iterable1 = ["a", "b", "c", "a", "e", "f", "a"]
result = []

for item in iterable1:
    if item == "a":
        result.append(item + "FOO")
    else:
        result.append(item)


print(result)
# ['aFOO', 'b', 'c', 'aFOO', 'e', 'f', 'aFOO']

You can express this as a list comprehension as follows:

iterable1 = ["a", "b", "c", "a", "e", "f", "a"]
result = [item + "FOO" if item == "a" else item for item in iterable1]

print(result)
# ['aFOO', 'b', 'c', 'aFOO', 'e', 'f', 'aFOO']

The two examples above are exactly interchangeable - neither is superior to the other - they're just different ways of saying the same thing.

If you wanted to use map(), you could do it as follows:

iterable1 = ["a", "b", "c", "a", "e", "f", "a"]
conditional1 = lambda item: item == "a"
expression1 = lambda item: item + "FOO" if conditional1(item) else item
result = list(map(expression1, iterable1))

print(result)
# ['aFOO', 'b', 'c', 'aFOO', 'e', 'f', 'aFOO']

Using map() is meaningfully different from using a loop - but using a list comprehension is no different from using a loop.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.