Use this tag for questions about the usage and features of More Itertools - the library that provides additional functions for working with Python iterables. When using this tag also include the more generic [python] tag where possible.
Questions tagged [more-itertools]
26 questions
10
votes
1 answer
Invalid syntax in more-itertools when running pytest
I have the following minimal setup.py:
import setuptools
setuptools.setup(
setup_requires=['pytest-runner'],
tests_require=['mock', 'pytest'],
test_suite='tests',
python_requires='>=2.7',
)
when running it with
python setup.py…

dzieciou
- 4,049
- 8
- 41
- 85
4
votes
3 answers
How to elegantly generate all prefixes of an iterable? (cumulative iterable)
From an iterable, I'd like to generate an iterable of its prefixes (including the original iterable itself).
for prefix in prefixes(range(5)):
print(tuple(prefix))
should result in
(0,)
(0, 1)
(0, 1, 2)
(0, 1, 2, 3)
(0, 1, 2, 3, 4)
or…

das-g
- 9,718
- 4
- 38
- 80
3
votes
2 answers
How can i return the longest continuous occurrence of "True" in Boolean, and replace other True with False?
I am trying to return a boolean which only gives the longest "True" occurrence in the original boolean and replace shorter "True" chunks into "False". Example a=[True, True, False, True , True, True, False], i want to return [False, False, False,…

Jamal
- 45
- 9
3
votes
2 answers
Python join more_itertools.windowed results
I have a following problem: I am trying to create so-called "digrams", like this:
If I have a word foobar, I want to get a list or a generator like: ["fo", "oo", "ob", "ba", "ar"]. The perfect function to that is more_itertools.windowed. The problem…

dabljues
- 1,663
- 3
- 14
- 30
2
votes
1 answer
All possible paths from pair combinations
I don't know the proper name for this connected component, yet what I'm looking for is to form paths with all possible combinations of a list of pairs/edges. Given a list:
("A", "B")
("A", "C")
("A", "D")
("C", "D")
("D", "B")
("D", "E")
("E",…

admirabilis
- 2,290
- 2
- 18
- 33
2
votes
1 answer
Skipping an Iteration in a Specific List when Iterating through Multiple Lists
This question is somewhat similar to this question, but different in that it entails multiple lists:
I have three lists:
a = [1, 2, 3, 4, 5, 6, 7]
b = ['ball', 'cat', 'dog', 'elephant', 'baboon', 'crocodile']
c = [6, 3, 5, 4, 3, 2, 1]
I am…

john_mon
- 487
- 1
- 3
- 13
2
votes
3 answers
Pure python or itertools group a list of dates by days difference between each date
Having a list of dates ordered:
[
datetime.date(2006, 8, 15),
datetime.date(2006, 9, 12),
datetime.date(2007, 8, 10),
datetime.date(2021, 4, 6),
datetime.date(2021, 4, 16),
datetime.date(2021, 4, 19)
...
]
I would like to have groups that contain…

lsgrep
- 33
- 3
2
votes
1 answer
Can't `pip install numpy` with a Python 3.9 venv
I installed Python 3.9, created a venv and tried to install numpy. There was an error with Cython, that was fixed in trunk.
So I installed in the venv Cython 3.0a0 and retried:
(venv) marco@buzz:~/sources/cython$ pip install numpy…

Marco Sulla
- 15,299
- 14
- 65
- 100
1
vote
1 answer
efficiently listing distinctive permutations that meet 'adjecancy requirements'
I'm trying to store distinctive permutations that meet additional (adjecancy) requirements. Below is a figure to illustrate the problem I am trying to solve.
I'm trying to make code that lists all distinctive permutations while taking adjecancy…

bprsch
- 67
- 6
1
vote
1 answer
Replace two characters in tuple element with dictionary value
I have a list of tuples (lot):
lot = [('490001', 'A-ARM1'),
('490001', 'A-ARM2'),
('490002', 'B-ARM3')]
Subsequently, I loop through every second tuple element and wish to replace every 'A-' and 'B-' characters by a dictionary…

Jeromekem
- 85
- 9
1
vote
4 answers
In Python, how do I iterate over previous, current and next values of a given list?
I don't even know where to begin to provide an adequate title for this question. Please suggest something better if you can.
EDIT:
Sorry, I need to clarify my question. I'm looking for something that is:
* memory efficient (i.e., using itertools,…

gskluzacek
- 113
- 7
1
vote
0 answers
Looking for something like more_itertools random_product, but it must consider each iteration only once
I'm not sure if this is even possible, but basically I'm looking for something like "random_product" from more_itertools but I would like it to consider each iteration only once -- yet still consider all of them. It also has to be done…

jlEe
- 41
- 2
0
votes
1 answer
(more_)itertools function to group (predicate true/false) tuples with group borders at first item in group
I'm looking for an iterator preferably from itertools or more_itertools that groups my_list such that each group starts at the first even digit that follows after an odd digit (or has no predecessor).
The single yielded groups shall also contain the…

koks der drache
- 1,398
- 1
- 16
- 33
0
votes
0 answers
Correct type annotation for a peekable iterator from more-itertools?
This code doesn't type-check:
def parse_expression(lex: Iterator[Token]) -> Expression:
left = parse_term(lex)
if lex.peek(): # ERROR
# ...
The error message is Cannot access member "peek" for type "Iterator[Token]", which is…

corazza
- 31,222
- 37
- 115
- 186
0
votes
2 answers
How to generate permutations without repetition
I have a table that looks like below
Loc
ID
filter
P1
A
ABC1
GHY
55.6
A
DFT1
FGH
67.8
B
HJH5
GHY
67
C
HKL
BHY
78
B
GTY
FGH
60
I want the output as below. Basically, I want the records with the same Filter to be one…

Sai
- 47
- 1
- 2
- 7