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
13
votes
6 answers

Cyclical Sliding Window Iteration

Consider some given sequence and a window length, say the list a = [13 * i + 1 for i in range(24)] (so that In [61]: a Out[61]: [1, 14, 27, 40, ..., 287, 300] ) and window length 3. I'd like to take the sliding window sum of this sequence,…
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
13
votes
8 answers

cycle through multiple list using itertools.cycle()

I have a list of servers. Every server has a list of name on it. example: server1 = ['a','b','c'] server2 = ['d','e','f'] server3 = ['g','h','i'] I want to iterate per server name not per server. For example after picking 'a' in server1, move to…
unice
  • 2,655
  • 5
  • 43
  • 78
13
votes
3 answers

itertools.product slower than nested for loops

I am trying using the itertools.product function to make a segment of my code (in an isotopic pattern simulator) easier to read and hopefully faster as well (the documentation states that no intermediate results are created) , I have however tested…
Bas Jansen
  • 3,273
  • 5
  • 30
  • 66
13
votes
6 answers

Group consecutive integers and tolerate gaps of 1

In Python, given a list of sorted integers, I would to group them by consecutive values and tolerate gaps of 1. For instance, given a list my_list: In [66]: my_list Out[66]: [0, 1, 2, 3, 5, 6, 10, 11, 15, 16, 18, 19, 20] I would like the following…
Ricky Robinson
  • 21,798
  • 42
  • 129
  • 185
13
votes
3 answers

Powersets in Python using itertools

I'm trying to create a powerset in Python 3. I found a reference to the itertools module, and I've used the powerset code provided on that page. The problem: the code returns a reference to an itertools.chain object, whereas I want access to the…
BruceM
  • 319
  • 1
  • 3
  • 8
13
votes
2 answers

Equivalent Nested Loop Structure with Itertools

Python's succint syntax through its batteries allows verbose code line to be expressed in readable one liners. Consider the following examples ====================================================| for a in range(3): …
Abhijit
  • 62,056
  • 18
  • 131
  • 204
13
votes
4 answers

itertools.islice implementation -- efficiently slicing a list

Earlier, I was trying to answer a question where I wanted to iterate over a list slice as efficiently as possible. for x in lst[idx1:]: isn't ideal as it creates a copy (In general, this is O(n)). My next thought was to use itertools.islice. But…
mgilson
  • 300,191
  • 65
  • 633
  • 696
12
votes
4 answers

Simple idiom to break an n-long list into k-long chunks, when n % k > 0?

In Python, it is easy to break an n-long list into k-size chunks if n is a multiple of k (IOW, n % k == 0). Here's my favorite approach (straight from the docs): >>> k = 3 >>> n = 5 * k >>> x = range(k * 5) >>> zip(*[iter(x)] * k) [(0, 1, 2), (3,…
kjo
  • 33,683
  • 52
  • 148
  • 265
12
votes
3 answers

Permutation without duplicates in Python

I have N positions, and each position can be either 0 or 1. I have fixed number of 1s, and I want to permutate these fixed number of 1s in these N positions. from itertools import permutations p = [0 for k in xrange(6)] for k in xrange(0,3): …
JoeJackJessieJames
  • 679
  • 3
  • 8
  • 12
12
votes
2 answers

What is itertools.groupby() used for?

While reading the python documentation I came across the itertools.groupby() function. It was not very straightforward so I decided to look up some info here on stackoverflow. I found something from How do I use Python's itertools.groupby()?. There…
chidimo
  • 2,684
  • 3
  • 32
  • 47
12
votes
3 answers

Difference between chain(*iterable) vs chain.from_iterable(iterable)

I have been really fascinated by all the interesting iterators in itertools, but one confusion I have had is the difference between these two functions and why chain.from_iterable exists. from itertools import chain def foo(n): for i in…
costrouc
  • 3,045
  • 2
  • 19
  • 23
12
votes
3 answers

How to get all combinations of a list?

I know that I can use itertools.permutation to get all permutations of size r. But, for itertools.permutation([1,2,3,4],3) it will return (1,2,3) as well as (1,3,2). I want to filter those repetitions (i.e obtain combinations) Is there a simple…
Bush
  • 2,433
  • 5
  • 34
  • 57
11
votes
1 answer

Python itertools.combinations' results

I don't get the number of results I should obtain from that function in the Title, so I'm hoping in your help. Looking at the Docs http://docs.python.org/library/itertools.html#itertools.combinations the number of results should be The number of…
Paolo
  • 1,299
  • 3
  • 15
  • 17
11
votes
7 answers

How do I reverse an itertools.chain object?

My function creates a chain of generators: def bar(num): import itertools some_sequence = (x*1.5 for x in range(num)) some_other_sequence = (x*2.6 for x in range(num)) chained = itertools.chain(some_sequence, some_other_sequence) …
Steven T. Snyder
  • 5,847
  • 4
  • 27
  • 58
11
votes
3 answers

Initialise a NumPy array based on its index

I am creating a couple of multi dimensional arrays using NumPy and inititalising them based on the index as follows: pos_data = [] # Some typical values d = 2 # Could also be 3 vol_ext = (1000, 500) # If d = 3, this will have another entry ratio =…
Luca
  • 10,458
  • 24
  • 107
  • 234