OKPEDIA PYTHON LISTA EN

How to convert an iterator to list in Python

To transform an iterator into a list in the Python language, use the list() function

list(x)

The parameter x is an iterator object.

The list() function transforms the iterator to a list.

Note. If the iterator object has infinite elements, the transformation causes an infinite loop.

Example

Given the following iterator

>>> iterator1=iter(["A","B","C","D"])

Convert iterator to a list with function list()

>>> y=list(iterator1)

The contents of the iterator are stored in the variable y as a list

>>> y
['A', 'B', 'C', 'D']

This operation reads and empties the iterator at once.

https://how.okpedia.org/en/python/how-to-convert-an-iterator-to-list-in-python


Report us an error or send a suggestion to improve this page


Python List


FacebookTwitterLinkedinLinkedin