Python list comprehension vs. nested loop, conciseness/efficiency
<p>The following 2 methods do the same thing. Which one is more efficient in terms of time/space complexity?</p>

<pre><code>** Method A**
for student in group.students:
    for grade in student.grades:
        some_operation(grade)

** Method B**
for grade in [grade for student in group.students for grade in student.grades]
    some_operation(grade)
</code></pre>

<p>These have the same time complexity, <code>O(nm)</code>, since it's a loop over another loop. So, the <code>n</code> is <code>group.students</code>, and <code>m</code> is <code>students.grades</code>. Functionally, these should be the same time complexity too since it's iterating over both lists either way.</p>

python code-complexity