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

List comprehension to extract multiple fields from list of tuples

I have a list of tuples servers = [('server1', 80 , 1, 2), ('server2', 443, 3, 4)] I want to create a new list that only has the first two fields as in: [('server1', 80), ('server2', 443)] but I cannot see how to craft a list comprehension for…
Bill
  • 618
  • 6
  • 15
22
votes
4 answers

How not to miss the next element after itertools.takewhile()

Say we wish to process an iterator and want to handle it by chunks. The logic per chunk depends on previously-calculated chunks, so groupby() does not help. Our friend in this case is itertools.takewhile(): while True: chunk =…
Paul Oyster
  • 1,133
  • 1
  • 12
  • 21
22
votes
7 answers

Replace list of list with "condensed" list of list while maintaining order

I have a list of list as in the code I attached. I want to link each sub list if there are any common values. I then want to replace the list of list with a condensed list of list. Examples: if I have a list [[1,2,3],[3,4]] I want [1,2,3,4]. If I…
Keith
  • 361
  • 1
  • 5
  • 18
21
votes
2 answers

itertools.groupby in a django template

I'm having an odd problem using itertools.groupby to group the elements of a queryset. I have a model Resource: from django.db import models TYPE_CHOICES = ( ('event', 'Event Room'), ('meet', 'Meeting Room'), # etc ) class…
Ismail Badawi
  • 36,054
  • 7
  • 85
  • 97
21
votes
6 answers

itertools product speed up

I use itertools.product to generate all possible variations of 4 elements of length 13. The 4 and 13 can be arbitrary, but as it is, I get 4^13 results, which is a lot. I need the result as a Numpy array and currently do the following: c =…
Christoph
  • 1,580
  • 5
  • 17
  • 29
21
votes
2 answers

How to get all mappings between two lists?

We have two lists, A and B: A = ['a','b','c'] B = [1, 2] Is there a pythonic way to build the set of all maps between A and B containing 2^n (here 2^3=8)? That is: [(a,1), (b,1), (c,1)] [(a,1), (b,1), (c,2)] [(a,1), (b,2), (c,1)] [(a,1), (b,2),…
Jean-Pat
  • 1,839
  • 4
  • 24
  • 41
21
votes
6 answers

Remove duplicate tuples from a list if they are exactly the same including order of items

I know questions similar to this have been asked many, many times on Stack Overflow, but I need to remove duplicate tuples from a list, but not just if their elements match up, their elements have to be in the same order. In other words, (4,3,5) and…
5813
  • 1,073
  • 3
  • 14
  • 28
20
votes
7 answers

Remove combinations that contains some values before even calculated

given a list and exclusions elements, is it possible to ignore calculation of combinations that contains these elements ? Example 1 Given l = [1, 2, 3, 4, 5], I want to calculate all combinations of size 4 and excluding combinations that contains…
syedelec
  • 1,285
  • 2
  • 19
  • 30
20
votes
6 answers

Filter a Set for Matching String Permutations

I am trying to use itertools.permutations() to return all the permutations of the string and return only the ones which are members of a set of words. import itertools def permutations_in_dict(string, words): ''' Parameters ---------- …
Meruemu
  • 611
  • 1
  • 8
  • 28
19
votes
5 answers

All possible ways to interleave two strings

I am trying to generate all possible ways to interleave any two arbitrary strings in Python. For example: If the two strings are 'ab' and 'cd', the output I wish to get is: ['abcd', 'acbd', 'acdb', 'cabd', 'cadb', 'cdab'] See a is always before b…
Seb
  • 481
  • 2
  • 6
  • 18
19
votes
2 answers

Python itertools permutations how to include repeating characters

I have some code like: def perm(n,i): b = 0 while b < n: n= n -1 from itertools import permutations as p file.write('\n'.join([''.join(item) for item in p(i,n)])) perm(4,'0123') which produces output…
jkdba
  • 2,378
  • 3
  • 23
  • 33
19
votes
9 answers

Solving Puzzle in Python

I got one puzzle and I want to solve it using Python. Puzzle: A merchant has a 40 kg weight which he used in his shop. Once, it fell from his hands and was broken into 4 pieces. But surprisingly, now he can weigh any weight between 1 kg to 40…
Abid Rahman K
  • 51,886
  • 31
  • 146
  • 157
18
votes
5 answers

What is the current value of a Python itertools counter

The itertools.count counter in Python (2.7.9) is very handy for thread-safe counting. How can I get the current value of the counter though? The counter increments and returns the last value every time you call next(): import itertools x =…
Carl
  • 1,027
  • 1
  • 9
  • 21
18
votes
1 answer

Numpy equivalent of itertools.product

I know about itertools.product for iterating on a list of several dimensions of keywords. For instance if I have this: categories = [ [ 'A', 'B', 'C', 'D'], [ 'E', 'F', 'G', 'H'], [ 'I', 'J', 'K', 'L'] ] and I use itertools.product()…
Jivan
  • 21,522
  • 15
  • 80
  • 131
17
votes
2 answers

Finding all combinations based on multiple conditions for a large list

I am trying to calculate the optimal team for a Fantasy Cycling game. I have a csv-file containing 176 cyclist, their teams, the amount of points they have scored and the price to put them in my team. I am trying to find the highest scoring team of…