Skip to main content
How are we doing? Please help us improve Stack Overflow. Take our short survey

All Questions

1 vote
3 answers
52 views

Iteration Functionality

Below is the simple programme which I wrote in Python Animal = ['tiger','lion','dog','cat'] xyz = iter(Animal) print(next(xyz)) The output was tiger Now I read that iter() method points towards the ...
Brijesh's user avatar
  • 45
1 vote
1 answer
59 views

For loop creates only one array instead of multiple

I have binary images containing different numbers of objects and I have to count the number of spots (the coordinates of these spots come from csv files) inside each object. The code I have works when ...
Carmen Grandi's user avatar
0 votes
0 answers
269 views

Implementing the range object as an iterator using generator function

I'm new to Python and I am trying to implement the range() object with positive step values as an iterator using a generator function in Python. I have already implemented this iterator by defining a ...
Anmol Saksena's user avatar
1 vote
1 answer
303 views

Why not make all iterables their own multiple pass iterators?

I'm new to Python, but my understanding of iterables and iterators in Python is that they support the iterator protocol. In some iteration tool/context, an iterable is passed to iter() to return an ...
bhp's user avatar
  • 121
0 votes
2 answers
44 views

Why is a variable not getting assigned to when assignments exist in both clauses of an if-else block?

Suppose we have something like: if True: r = 0 else: r = 1 print(r) Why would we get UnboundLocalError: local variable 'r' referenced before assignment? The actual code is shown below:...
Toothpick Anemone's user avatar
0 votes
3 answers
34 views

How to define two ways of iterate according input

What is the most pythonic form to define two ways of iterating. In example, I have this original code: def f1(cat_gen): for (a, b), c in cat_gen: if some condition: yield (a, ...
Joaquin's user avatar
  • 2,101
1 vote
1 answer
661 views

How do I iterate through two lists in API call Python 3

I have two files containing information that I need to input in the same script. One containing ID's, one on each line, and the other list containing parameters on their own individual lines as well. ...
NetworkingCat's user avatar
1 vote
0 answers
49 views

Python3.6: Use import os, iter and next to print directory of files with the same extension

Python 3.6 import os files = os.open('dir *.py') file_iter = iter(files) print(next(file_iter)) #Output: Volume in drive C is c However when I use a for loop: for file in files: print((file), ...
notabot62's user avatar
0 votes
1 answer
3k views

Return average / mean of a specific key in a list of dictionaries

This is probably a duplicate, so please let me know if I didn't find something. My question is in the title. I know that the following code would do it, but I don't think this is how it should be ...
Jacob Birkett's user avatar
2 votes
1 answer
256 views

Loop for `times` if `times is not None` else loop forever

Suppose I have a function of the form def foo(times=None): iterator = range(times) if times else itertools.count() for _ in iterator: # do stuff Is there a more Pythonic or elegant ...
Elliot Gorokhovsky's user avatar
8 votes
1 answer
689 views

Why does map over an iterable return a one-shot iterable?

Why does map when called with an object which can be iterated over multiple times not return an object which can also be iterated multiple times? I think the latter is much more reasonable. My use ...
Manuel Schmidt's user avatar
2 votes
1 answer
464 views

Force all iterations on an iterable

I've written a for-loop using map, with a function that has a side-effect. Here's a minimal working example of what I mean: def someFunc(t): n, d = t d[n] = str(n) def main(): d = {} ...
inspectorG4dget's user avatar
2 votes
1 answer
58 views

why reversed() list iteration with step doesn't work properly?

def checkio(data): s1=[] s2=[] data.sort() for x in reversed(data[0:len(data):2]): s1.append(x) for y in reversed(data[0:(len(data)-1):2]): s2.append(y) print(s1, s2) ...
Reloader's user avatar
  • 718