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
678
votes
15 answers

How do I use itertools.groupby()?

I haven't been able to find an understandable explanation of how to actually use Python's itertools.groupby() function. What I'm trying to do is this: Take a list - in this case, the children of an objectified lxml element Divide it into groups…
James Sulak
  • 31,389
  • 11
  • 53
  • 57
277
votes
11 answers

How can I match up permutations of a long list with a shorter list (according to the length of the shorter list)?

I’m having trouble wrapping my head around a algorithm I’m try to implement. I have two lists and want to take particular combinations from the two lists. Here’s an example. names = ['a', 'b'] numbers = [1, 2] the output in this case would…
user1735075
  • 3,221
  • 4
  • 16
  • 16
97
votes
5 answers

All permutations of a Windows license key

I need to apply for a Windows 8 upgrade for my laptop, for which I need the Windows 7 license key on the underside of the laptop. Because Microsoft decided in their infinite wisdom to create license labels that wear off, and I cannot read my license…
Kerridge0
  • 2,017
  • 19
  • 22
88
votes
20 answers

permutations with unique values

itertools.permutations generates where its elements are treated as unique based on their position, not on their value. So basically I want to avoid duplicates like this: >>> list(itertools.permutations([1, 1, 1])) [(1, 1, 1), (1, 1, 1), (1, 1, 1),…
xyz-123
  • 2,227
  • 4
  • 21
  • 27
88
votes
6 answers

What is the difference between chain and chain.from_iterable in itertools?

I could not find any valid example on the internet where I can see the difference between them and why to choose one over the other.
user1994660
  • 5,253
  • 11
  • 30
  • 33
85
votes
4 answers

When is it better to use zip instead of izip?

When is it better to use zip instead of itertools.izip?
Neil G
  • 32,138
  • 39
  • 156
  • 257
84
votes
3 answers

importing izip from itertools module gives NameError in Python 3.x

I am trying to import the izip module like so: from itertools import izip However after recently changing over from Python 2.7 to 3 - it doesn't seem to work. I am trying to write to a csv file: writer.writerows(izip(variable1,2)) But I have no…
Enigmatic
  • 3,902
  • 6
  • 26
  • 48
66
votes
8 answers

Zipped Python generators with 2nd one being shorter: how to retrieve element that is silently consumed

I want to parse 2 generators of (potentially) different length with zip: for el1, el2 in zip(gen1, gen2): print(el1, el2) However, if gen2 has less elements, one extra element of gen1 is "consumed". For example, def my_gen(n:int): for i in…
Jean-Francois T.
  • 11,549
  • 7
  • 68
  • 107
61
votes
7 answers

Python how to read N number of lines at a time

I am writing a code to take an enormous textfile (several GB) N lines at a time, process that batch, and move onto the next N lines until I have completed the entire file. (I don't care if the last batch isn't the perfect size). I have been reading…
brokentypewriter
  • 787
  • 2
  • 6
  • 8
59
votes
3 answers

Python: How to get the length of itertools _grouper

I'm working with Python itertools and using groupby to sort a bunch of pairs by the last element. I've gotten it to sort and I can iterate through the groups just fine, but I would really love to be able to get the length of each group without…
user1466679
  • 593
  • 1
  • 4
  • 4
56
votes
3 answers

How to apply itertools.product to elements of a list of lists?

I have a list of arrays and I would like to get the cartesian product of the elements in the arrays. I will use an example to make this more concrete... itertools.product seems to do the trick but I am stuck in a little detail. arrays = [(-1,+1),…
W7GVR
  • 1,990
  • 1
  • 18
  • 24
48
votes
2 answers

itertools.cycle().next()?

Well, I was using itertools.cycle().next() method with Python 2.6.6, but now that I updated to 3.2 I noticed that itertools.cycle() object has no method next(). I used it to cycle a string in the spin()method of a Spinner class. So if we cycle the…
Paulo Freitas
  • 13,194
  • 14
  • 74
  • 96
48
votes
6 answers

zip iterators asserting for equal length in python

I am looking for a nice way to zip several iterables raising an exception if the lengths of the iterables are not equal. In the case where the iterables are lists or have a len method this solution is clean and easy: def zip_equal(it1, it2): if…
zeehio
  • 4,023
  • 2
  • 34
  • 48
40
votes
6 answers

I can't find imap() in itertools in Python 3

I have a problem that I want to solve with itertools.imap(). I imported itertools and called itertools.imap(), but apparently itertools doesn't have attribute imap. What's going wrong? >>> import itertools >>> dir(itertools) ['__doc__',…
user3905370
38
votes
1 answer

Python 3, module 'itertools' has no attribute 'ifilter'

I am new at Python, trying to build an old python file into Python 3. I got several build errors which I solved. But at this point I am getting above error. I have no idea how to fix this. The code section looks like below. return…
Sohag Mony
  • 597
  • 2
  • 8
  • 16
1
2 3
99 100