Tagged Questions
113
votes
10answers
43k views
Remove items from a list while iterating in Python
I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria.
for tup in somelist:
if determine(tup):
code_to_remove_tup
What should I ...
48
votes
8answers
19k views
Iterating through a range of dates in Python
I have the following code to do this, but how can I do it better? Right now I think it's better than nested loops, but it starts to get Perl-one-linerish when you have a generator in a list ...
15
votes
7answers
5k views
Iteration over list slices
Good day function-wizards,
I want an algorithm to iterate over list slices. Slices size is set outside the function and can differ.
In my mind it is something like:
for list_of_x_items in ...
23
votes
12answers
12k views
Iterate over pairs in a list (circular fashion) in Python
The problem is easy, I want to iterate over each element of the list and the next one in pairs (wrapping the last one with the first).
I've thought about two unpythonic ways of doing it:
def ...
10
votes
12answers
5k views
Iterate over a python sequence in multiples of n?
How do I process the elements of a sequence in batches, idiomatically?
For example, with the sequence "abcdef" and a batch size of 2, I would like to do something like the following:
for x, y in ...
9
votes
10answers
784 views
Is there a map without result in python?
Sometimes, I just want to execute a function for a list of entries -- eg.:
for x in wowList:
installWow(x, 'installed by me')
Sometimes I need this stuff for module initialization, so I don't ...
81
votes
6answers
25k views
How do I use Python's 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 ...
51
votes
7answers
49k views
How can I reverse a list in python?
How can i do this in python?
array=[0,10,20,40]
for (i = array.length() - 1 ;i >= 0; i--)
I need to have the elements of an array but from the end to the beginning.
7
votes
7answers
5k views
Remove items from a list while iterating without using extra memory in Python
My problem is simple: I have a long list of elements that I want to iterate through and check every element against a condition. Depending on the outcome of the condition I would like to delete the ...
3
votes
4answers
3k views
Iterative find/replace from a list of tuples in Python
I have a list of tuples, each containing a find/replace value that I would like to apply to a string. What would be the most efficient way to do so? I will be applying this iteratively, so performance ...
3
votes
3answers
138 views
Grouping / clustering numbers in Python
I've googled, I've tested, and this has me at my wits end. I have a list of numbers I need to group by similarity. For instance, in a list of [1, 6, 9, 100, 102, 105, 109, 134, 139], 1 6 9 would be ...
22
votes
5answers
13k views
Converting a List of Tuples into a Dict in Python
I have a list of tuples like this:
[
('a', 1),
('a', 2),
('a', 3),
('b', 1),
('b', 2),
('c', 1),
]
I want to iterate through this keying by the first item, so for example I could print something ...
9
votes
9answers
8k views
Python: Adding element to list while iterating
I know that it is not allowed to remove elements while iterating a list, but is it allowed to add elements to a python list while iterating. Here is an example:
for a in myarr:
if ...
5
votes
3answers
2k views
pairwise traversal of a list or tuple
a = [5, 66, 7, 8, 9, ...]
Is it possible to make an iteration instead of writing like this?
a[1] - a[0]
a[2] - a[1]
a[3] - a[2]
a[4] - a[3]
...
Thank you!
8
votes
4answers
660 views
for x in y, type iteration in python. Can I find out what iteration I'm currently on?
I have a question about the loop construct in Python in the form of: for x in y: In my case y is a line read from a file and x is separate characters. I would like to put a space after every pair of ...