Pythonic way to unpack an iterator inside of a list

后端 未结 5 930
醉酒成梦
醉酒成梦 2020-12-30 16:24

I\'m trying to figure out what is the pythonic way to unpack an iterator inside of a list.

For example:

my_iterator = zip([1, 2, 3, 4], [1, 2, 3, 4]         


        
5条回答
  •  梦谈多话
    2020-12-30 16:47

    This might be a repeat of Fastest way to convert an iterator to a list, but your question is a bit different since you ask which is the most Pythonic. The accepted answer is list(my_iterator) over [e for e in my_iterator] because the prior runs in C under the hood. One commenter suggests [*my_iterator] is faster than list(my_iterator), so you might want to test that. My general vote is that they are all equally Pythonic, so I'd go with the faster of the two for your use case. It's also possible that the older answer is out of date.

提交回复
热议问题