3 ms·
If you look at the disassembly, there's no difference in python 3 (I only tested 3.7+), so it's a matter of style. There used to be a difference in python2 --
by tomn 4y ago
If you look at the disassembly, there's no difference in python 3 (I only tested 3.7+), so it's a matter of style.
There used to be a difference in python2 -- the list option would turn into n LOAD_CONSTs and a BUILD_LIST, whereas the tuple option would just emit one LOAD_CONST.
import dis
def with_tuple():
for i in (1,2,3):
print(i)
def with_list():
for i in [1,2,3]:
print(i)
print("with_tuple")
dis.dis(with_tuple)
print("with_list")
dis.dis(with_list)
edit: looks like this changed in python 3.6: https://python.godbolt.org/z/rEfY9GT9W https://python.godbolt.org/z/rEfY9GT9W