If you want to make your code more efficient, it is important, that you state with respect to what you want to make it more efficient. Besides terrible solutions, there is often a trade-off between space (memory) and time (cycles, functions calls) among the reasonable solutions.
Aside from efficiency, you should also take readability and maintainability into account. Before doing any kind of optimizations.
Tuples like dicts in Python are very efficient, because they are used internally all over place. Most function calls in Python involve tuple creation (for positional arguments) under the hood.
As to your concrete example, you can use a generator expression to avoid the temporary list:
entries = ((k, v) for k, l in dic.items() for v in l)
You still have the intermediate tuples, but they are computed on the fly, while you iterate over the dictionary items. This solution would be more memory efficient than an explicit list, especially if you have lots of entries.
You could also just put the nested loop directly into the with body:
with open(index_filename,'wb') as out:
csv_out=csv.writer(out, delimiter='|')
csv_out.writerow(['identifier','descriptor'])
for k, v in dic.items():
for ID in v:
csv_out.writerow((k, ID))
To avoid the repeated function calls to writerow
, you could also resort to writerows
, which might be faster.
with open(index_filename,'wb') as out:
csv_out=csv.writer(out, delimiter='|')
csv_out.writerow(['identifier','descriptor'])
csv_out.writerows((k, v) for k, l in dic.items() for v in l)
If you are really interested in, which method is the fastest, you can use Python's timeit module to make measurements.