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
35
votes
4 answers

Why does itertools.permutations() return a list, instead of a string?

Why does itertools.permutations() return a list of characters or digits for each permutation, instead of just returning a string? For example: >>> print([x for x in itertools.permutations('1234')]) >>> [('1', '2', '3', '4'), ('1', '2', '4', '3'),…
kennysong
  • 2,044
  • 6
  • 24
  • 37
34
votes
6 answers

What is the purpose of Python's itertools.repeat?

For every use I can think of for Python's itertools.repeat() class, I can think of another equally (possibly more) acceptable solution to achieve the same effect. For example: >>> [i for i in itertools.repeat('example', 5)] ['example', 'example',…
Tyler Crompton
  • 12,284
  • 14
  • 65
  • 94
31
votes
3 answers

itertools.accumulate() versus functools.reduce()

In Python 3.3, itertools.accumulate(), which normally repeatedly applies an addition operation to the supplied iterable, can now take a function argument as a parameter; this means it now overlaps with functools.reduce(). With a cursory look, the…
JAB
  • 20,783
  • 6
  • 71
  • 80
30
votes
3 answers

itertools.groupby() not grouping correctly

I have this data: self.data = [(1, 1, 5.0), (1, 2, 3.0), (1, 3, 4.0), (2, 1, 4.0), (2, 2, 2.0)] When I run this code: for mid, group in itertools.groupby(self.data,…
user994165
  • 9,146
  • 30
  • 98
  • 165
30
votes
2 answers

Failing to import itertools in Python 3.5.2

I am new to Python. I am trying to import izip_longest from itertools. But I am not able to find the import "itertools" in the preferences in Python interpreter. I am using Python 3.5.2. It gives me the below error- from itertools import…
Joyita Das
  • 447
  • 1
  • 5
  • 12
30
votes
4 answers

How to get the length of an itertools.product?

I am using itertools to run a numerical simulation iterating over all possible combinations of my input parameters. In the example below, I have two parameters and six possible combinations: import itertools x = [0, 1] y = [100, 200, 300] myprod =…
Pythonista anonymous
  • 8,140
  • 20
  • 70
  • 112
29
votes
7 answers

How can I find all ways of pairing elements between two lists of equal length?

Suppose I have two input lists like: list1 = [1, 2, 3] list2 = [4, 5, 6] I want to get a list of lists of tuples, of all the potential combinations between the two - i.e., of ways to pair all the elements up. Thus, the result should be like: [ …
GeoJunkie
  • 489
  • 2
  • 5
  • 8
28
votes
5 answers

How to create a list of dictionaries from a dictionary with lists of different lengths

I want to create a list of dictionaries with the same index element from each list. I have this dictionary: d = {'name': ['bob', 'john', 'harry', 'mary'], 'age': [13, 19, 23], 'height': [164, 188], 'job': ['programmer']} The…
raul
  • 327
  • 2
  • 12
27
votes
1 answer

How to turn an itertools "grouper" object into a list

I am trying to learn how to use itertools.groupby in Python and I wanted to find the size of each group of characters. At first I tried to see if I could find the length of a single group: from itertools import groupby len(list(list(…
cafemolecular
  • 525
  • 2
  • 6
  • 13
27
votes
3 answers

Python itertools.combinations: how to obtain the indices of the combined numbers

The result created by Python's itertools.combinations() is the combinations of numbers. For example: a = [7, 5, 5, 4] b = list(itertools.combinations(a, 2)) # b = [(7, 5), (7, 5), (7, 4), (5, 5), (5, 4), (5, 4)] But I would like to also obtain the…
Randy Tang
  • 4,283
  • 12
  • 54
  • 112
26
votes
4 answers

N-D version of itertools.combinations in numpy

I would like to implement itertools.combinations for numpy. Based on this discussion, I have a function that works for 1D input: def combs(a, r): """ Return successive r-length combinations of elements in the array a. Should produce the…
endolith
  • 25,479
  • 34
  • 128
  • 192
25
votes
5 answers

implementing argmax in Python

How should argmax be implemented in Python? It should be as efficient as possible, so it should work with iterables. Three ways it could be implemented: given an iterable of pairs return the key corresponding to the greatest value given an…
Neil G
  • 32,138
  • 39
  • 156
  • 257
25
votes
5 answers

Concatenate tuples using sum()

From this post I learned that you can concatenate tuples with sum(): >>> tuples = (('hello',), ('these', 'are'), ('my', 'tuples!')) >>> sum(tuples, ()) ('hello', 'these', 'are', 'my', 'tuples!') Which looks pretty nice. But why does this work? …
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
25
votes
3 answers

Group by and aggregate the values of a list of dictionaries in Python

I'm trying to write a function, in an elegant way, that will group a list of dictionaries and aggregate (sum) the values of like-keys. Example: my_dataset = [ { 'date': datetime.date(2013, 1, 1), 'id': 99, 'value1':…
Kyle Getrost
  • 707
  • 1
  • 5
  • 11
23
votes
6 answers

Separating a String

Given a string, I want to generate all possible combinations. In other words, all possible ways of putting a comma somewhere in the string. For example: input: ["abcd"] output: ["abcd"] ["abc","d"] ["ab","cd"] …
Noob Coder
  • 949
  • 2
  • 10
  • 17