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
14
votes
3 answers

Why do I get a MemoryError with itertools.product?

I would expect the following snippet to give me an iterator yielding pairs from the Cartesian product of the two input iterables: $ python Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits"…
detly
  • 29,332
  • 18
  • 93
  • 152
14
votes
4 answers

itertools does not recognize numpy ints as valid inputs on Python 3.6

Take this code: import itertools as it import numpy as np data = ['a','b','c','d'] dw = np.array([1, 3], dtype=np.int64) print(list(it.islice(data,dw[0],dw[1],1))) On Python 2.7 it prints ['b', 'c',] as expected. On Python 3.6 it throws an…
Khris
  • 3,132
  • 3
  • 34
  • 54
14
votes
5 answers

Iterating over multiple indices with i > j ( > k) in a pythonic way

i need to iterate over a tuple of indices. all indices must be in the range [0, N) with the condition i > j. The toy example I present here deals with only two indices; I will need to extend that to three (with i > j > k) or more. The basic version…
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
14
votes
2 answers

What should itertools.product() yield when supplied an empty list?

I guess it's an academic question, but the second result does not make sense to me. Shouldn't it be as thoroughly empty as the first? What is the rationale for this behavior? from itertools import product one_empty = [ [1,2], [] ] all_empty =…
FMc
  • 41,963
  • 13
  • 79
  • 132
14
votes
4 answers

How can I use python itertools.groupby() to group a list of strings by their first character?

I have a list of strings similar to this list: tags = ('apples', 'apricots', 'oranges', 'pears', 'peaches') How should I go about grouping this list by the first character in each string using itertools.groupby()? How should I supply the 'key'…
Adam Ziolkowski
  • 153
  • 1
  • 1
  • 7
14
votes
1 answer

What are the Ruby equivalent of Python itertools, esp. combinations/permutations/groupby?

Python's itertools module provides a lots of goodies with respect to processing an iterable/iterator by use of generators. For example, permutations(range(3)) --> 012 021 102 120 201 210 combinations('ABCD', 2) --> AB AC AD BC BD CD [list(g) for…
Hai-Anh Trinh
  • 1,142
  • 11
  • 11
14
votes
3 answers

More efficient way to get integer permutations?

I can get integer permutations like this: myInt = 123456789 l = itertools.permutations(str(myInt)) [int(''.join(x)) for x in l] Is there a more efficient way to get integer permutations in Python, skipping the overhead of creating a string, then…
jumbopap
  • 3,969
  • 5
  • 27
  • 47
14
votes
1 answer

Python itertools.product with variable number of arguments

I am trying to write a module to combine a variable number of lists using itertools.product. The closest I can get is: import itertools lists = [["item1","item2"],["A","b","C"], ["etc..."]] searchterms = list(itertools.product(lists)) print…
ralph346526
  • 338
  • 3
  • 12
13
votes
4 answers

How to join/merge two generators output using python

I have two generators g1 and g2 for line in g1: print line[0] [a, a, a] [b, b, b] [c, c, c] for line1 in g2: print line1[0] [1, 1, 1] [2, 2, 2] [3, 3, 3] for line in itertools.chain(g1, g2): print line[0] [a, a, a] [b, b, b] [c, c,…
daikini
  • 1,307
  • 6
  • 23
  • 36
13
votes
3 answers

Combining itertools and multiprocessing?

I have a 256x256x256 Numpy array, in which each element is a matrix. I need to do some calculations on each of these matrices, and I want to use the multiprocessing module to speed things up. The results of these calculations must be stored in a…
digitaldingo
  • 519
  • 6
  • 11
13
votes
2 answers

Prevent memory error in itertools.permutation

Firstly I would like to mention that i have a 3 gb ram. I am working on an algorithm that is exponential in time on the nodes so for it I have in the code perm = list( itertools.permutations(list(graph.Nodes))) # graph.Nodes is a tuple of 1 , 2 ,…
user506710
13
votes
2 answers

How to make zip_longest available in itertools using Python 2.7

When trying to import this function on a Python Jupyter 2.7 nb running on Windows 10, I get this error: I believe I hadn't encountered problems in the past because I was using Python 3. So I wonder if it is just that it is not available in Python…
Antoni Parellada
  • 4,253
  • 6
  • 49
  • 114
13
votes
5 answers

Using 'reduce' on a list of dictionaries

I'm trying to write a simple Python function that sums all values that have the key as likes. I'm working with functional programming for this assignment. Thus, I am required to use either a list-comprehension, map, filter, or reduce. In this case,…
13
votes
3 answers

Faster numpy-solution instead of itertools.combinations?

I'm using itertools.combinations() as follows: import itertools import numpy as np L = [1,2,3,4,5] N = 3 output = np.array([a for a in itertools.combinations(L,N)]).T Which yields me the output I need: array([[1, 1, 1, 1, 1, 1, 2, 2, 2, 3], …
Khris
  • 3,132
  • 3
  • 34
  • 54
13
votes
10 answers

Split python tuple in subtuples with capacity limit in functional programming style

I have some tuple in python. And capacity limit, for example, is 5. I want to split tuple in subtuples limited by sum of them elements: For example: input: (3, 1, 4, 2, 2, 1, 1, 2) and capacity = 5 output: (3, 1) (4) (2, 2, 1) (1, 2) #each subtuple…
Evg
  • 2,978
  • 5
  • 43
  • 58