将迭代器转换为列表的最快方法

问题:将迭代器转换为列表的最快方法

有一个iterator对象,是否有比列表理解更快,更好或更正确的方法来获取迭代器返回的对象的列表?

user_list = [user for user in user_iterator]

Having an iterator object, is there something faster, better or more correct than a list comprehension to get a list of the objects returned by the iterator?

user_list = [user for user in user_iterator]

回答 0

list(your_iterator)
list(your_iterator)

回答 1

python 3.5开始, 您可以使用*可迭代的拆包运算符:

user_list = [*your_iterator]

pythonic的方法是:

user_list  = list(your_iterator)

since python 3.5 you can use * iterable unpacking operator:

user_list = [*your_iterator]

but the pythonic way to do it is:

user_list  = list(your_iterator)