Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store itertools.chain and use it more than once?

I would like to use itertools.chain for efficient concatenation of lists (memoization), but I need to be able to read (or map, etc.) the result multiple times. This example illustrates the problem:

import itertools
a = itertools.chain([1, 2], [3, 4])
print list(a) # => [1, 2, 3, 4]
print list(a) # => []

What is the best way to avoid this problem?

like image 996
jtbandes Avatar asked Oct 31 '12 11:10

jtbandes


People also ask

What is chain() function in itertools?

One such itertools function is chain (). It is a function that takes a series of iterables and returns one iterable. It groups all the iterables together and produces a single iterable as output. Its output cannot be used directly and thus explicitly converted into iterables. This function come under the category iterators terminating iterators.

What is the use of itertools in Python?

They make iterating through the iterables like lists and strings very easily. One such itertools function is chain(). Note: For more information, refer to Python Itertools chain() function. It is a function that takes a series of iterables and returns one iterable. It groups all the iterables together and produces a single iterable as output.

What is the difference between IZIP() and itertools list()?

import itertools list (itertools.islice (count (), 5)) # Output # [0, 1, 2, 3, 4] izip () returns an iterator that combines the elements of the passed iterators into tuples. It works similarly to zip (), but returns an iterator instead of a list.

Do I need New itertools functions to write this function?

You do not need any new itertools functions to write this function. See what you can come up with on your own before reading ahead. You start by creating a list of hand_size references to an iterator over deck.


1 Answers

As with all generators, you'll need to convert it to a list and store that result instead:

a = list(a)

This is a fundamental principle of generators, they are expected to produce their sequence only once.

Moreover, you cannot simply store a generator for memoization purposes, as the underlying lists could change. In almost all memoization use-cases, you should store the list instead; a generator is usually only a means of efficiently transforming or filtering the underlying sequences, and does not represent the data you want to memoize itself. It's as if you are storing a function, not it's output. In your specific case, if all what you are doing is using chain() to concatenate existing lists, store those lists directly instead.

Note that this enables generators to produce endless sequences, so be careful with that you convert to a list.

like image 128
Martijn Pieters Avatar answered Sep 22 '22 07:09

Martijn Pieters