Questions tagged [python-itertools]

A standard library module for Python with utilities for iterables. Also add the python tag for increased visibility. Use [rust-itertools] for the Rust crate.

itertools is a module for the Python language containing high level functional constructs for working with iterable objects.

When using this tag, do not forget to include the tag.


For the Rust itertools crate, use instead.

3065 questions
17
votes
2 answers

Performance of map vs starmap?

I was trying to make a pure-python (without external dependencies) element-wise comparison of two sequences. My first solution was: list(map(operator.eq, seq1, seq2)) Then I found starmap function from itertools, which seemed pretty similar to me.…
godaygo
  • 2,215
  • 2
  • 16
  • 33
17
votes
3 answers

itertools not defined when used inside module

I save my custom functions in a separate module that I can call when I need to. One of my new functions uses itertools, but I keep getting a name error. NameError: name 'itertools' is not defined It's really weird. I can import itertools in the…
Adam
  • 581
  • 2
  • 4
  • 12
17
votes
4 answers

what is the quickest way to iterate through a numpy array

I noticed a meaningful difference between iterating through a numpy array "directly" versus iterating through via the tolist method. See timing below: directly [i for i in np.arange(10000000)] via tolist [i for i in np.arange(10000000).tolist()]…
piRSquared
  • 285,575
  • 57
  • 475
  • 624
17
votes
1 answer

My IDLE does not recognize itertools.izip() as a function

>>> itertools.izip('ABCD', 'xy') Traceback (most recent call last): File "", line 1, in itertools.izip('ABCD', 'xy') AttributeError: 'module' object has no attribute 'izip'
Alexander Wright
  • 181
  • 1
  • 1
  • 4
17
votes
1 answer

How to group similar items in a list?

I am looking to group similar items in a list based on the first three characters in the string. For example: test = ['abc_1_2', 'abc_2_2', 'hij_1_1', 'xyz_1_2', 'xyz_2_2'] How can I group the above list items into groups based on the first…
Borealis
  • 8,044
  • 17
  • 64
  • 112
17
votes
4 answers

Cartesian product of large iterators (itertools)

From a previous question I learned something interesting. If Python's itertools.product is fed a series of iterators, these iterators will be converted into tuples before the Cartesian product begins. Related questions look at the source code of…
Hooked
  • 84,485
  • 43
  • 192
  • 261
16
votes
3 answers

Does python have a built-in function for interleaving generators/sequences?

I noticed that itertools does not (it seems to me) have a function capable of interleaving elements from several other iterable objects (as opposed to zipping them): def leaf(*args): return (it.next() for it in…
Marcin
  • 48,559
  • 18
  • 128
  • 201
16
votes
2 answers

Is itertools thread-safe?

For instance, if I create an iterator using chain, can I call it on multiple threads? Note that thread-safety that relies on the GIL is acceptable, but not preferable. (Note that this is a bit different from this question, which deals with…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
16
votes
1 answer

Why is itertools.chain faster than a flattening list comprehension?

In the context of a discussion in the comments of this question it was mentioned that while concatenating a sequence of strings simply takes ''.join([str1, str2, ...]), concatenating a sequence of lists would be something like…
jdehesa
  • 58,456
  • 7
  • 77
  • 121
16
votes
2 answers

zip_longest without fillvalue

I am searching for a middle ground between Python's zip and zip_longest functions (from the itertools module), that exhausts all given iterators, but does not fill in anything. So, for example, it should transpose tuples like so: (11, 12, 13 ), …
XZS
  • 2,374
  • 2
  • 19
  • 38
16
votes
1 answer

Itertools product without repeating duplicates

from itertools import product teams = ['india', 'australia', 'new zealand'] word_and = ['and'] tmp = '%s %s %s' items = [teams, word_and, teams] print(list(tmp % a for a in list(product(*items)))) prints: ['india and india', 'india and…
Raj
  • 22,346
  • 14
  • 99
  • 142
16
votes
3 answers

itertools.tee on a coroutine?

I have a tree structure of objects. I need to iterate over all the items ("values") in the leaves. For this I'm currently using generator methods as illustrated below: class Node(object): def __init__(self): self.items = [Leaf(1),…
Brecht Machiels
  • 3,181
  • 3
  • 25
  • 38
15
votes
3 answers

How to unzip an iterator?

Given a list of pairs xys, the Python idiom to unzip it into two lists is: xs, ys = zip(*xys) If xys is an iterator, how can I unzip it into two iterators, without storing everything in memory?
Grzegorz Chrupała
  • 3,053
  • 17
  • 24
15
votes
2 answers

Python's itertools product memory consumption

The documentation says that the cartesian product function the actual implementation does not build up intermediate results in memory. How can that be possible with generators? Can somebody show me an example with a bounded memory consumption for…
fulmicoton
  • 15,502
  • 9
  • 54
  • 74
14
votes
3 answers

Python permutations with constraints

I am using python 3 and I am trying to find a way to get all the permutations of a list while enforcing some constraints. For instance, I have a list L=[1, 2, 3, 4, 5, 6, 7] I want to find all permutations. However, My constraints are: 1 should…
Keeto
  • 4,074
  • 9
  • 35
  • 58