$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · medium · #100

Which fix removes the inefficiency?

out = ''
for row in rows:
    out += row + ','
  1. Build once with ','.join(rows) instead
  2. Build once with ','.append(rows) instead
  3. Change out = '' to out = [] only
  4. Use out = out + row inside the loop
Strings are immutable, so += rebuilds the whole string each pass; join builds it once.
Play Problem Solving ▸