This question already has answers here:
Why can't I iterate twice over the same iterator? How can I "reset" the iterator or reuse the data?
(5 answers)
Closed 9 years ago.
I wish to iterate over a list_iterator twice. When I currently try to do this, the iterator has nothing to iterate over the second time. Can I reset it?
l = iter(["1","2","3","4"])
for i in l:
print(i)
for i in l:
print(i)
A list_iter object is being passed to the function in which I wish to iterate twice in. Is it bad practive to pass around list_iterator objects?
You could always use itertools.tee to get copies of the iterable.
Ala
l = iter(["1","2","3","4"])
# Don't use original....
original, new = tee(l)
Related
This question already has answers here:
Why can't I iterate twice over the same iterator? How can I "reset" the iterator or reuse the data?
(5 answers)
Closed 4 years ago.
I am new to python and was learning its builtins but function all()
is not behaving as expected i don't know why.Here is my code
n=map(int,input().strip().split())
print(all([j>0 for j in n]))
print(list(n)) #this line returning empty list
Here are my inputs:
1 2 3 4 5 -9
And my output:
False
Does function all changes the original map object(values)? but something like this is not mentioned in given function definition on the docs link.
Thanks in advance
Map returns a generator object which you exhausted in your all function. Therefore when you call list on n, since n is empty/exhausted, it returns an empty list.
To fix just make n a list in the first place.
n=list(map(int,input().strip().split()))
print(all([j>0 for j in n]))
print(n)
This question already has answers here:
Preventing function to not stop at first `return` within the `for` loop [duplicate]
(4 answers)
What is the purpose of the return statement? How is it different from printing?
(15 answers)
Closed 4 years ago.
My intention is to get [[1.23,2.45],[2.35,9.87]).
The following gives one single element to be 1.23, why is this so?
b =[[1.234,2.454],[2.352,9.873]]
def get_round(x):
for i in x:
if type(i) ==list:
for each_i in i:
return round(each_i,2)
else:
return round(i,2)
get_round(b)
How can I round every single elements and without change the existing data structure?
Here is a nice pythonic way to do this
func = lambda x: round(x,2)
b =[[1.234,2.454],[2.352,9.873]]
b = [list(map(func, i)) for i in b]
print(b)
[[1.23, 2.45], [2.35, 9.87]]
This will create a temporary function which will round a single value to 2 decimal places. We call this function func. Now we want to apply this function to each list in a list so we iterate through our outer list and extract each list inside it. Then we use the map function to apply func to every single element inside this list. We then convert our map iteratable object to a list and we have our answer.
Doing things your way
b =[[1.234,2.454],[2.352,9.873]]
def get_round(x):
temp = []
for i in x:
if type(i) ==list:
x = []
for each_i in i:
x.append(round(each_i,2))
temp.append(x)
else:
temp.append(round(i,2))
return temp
print(get_round(b))
In your original code you were returning too soon. You find the first number in your list of lists, convert it, then return it. This goes back to where we called the function from.
This question already has answers here:
How to remove multiple indexes from a list at the same time? [duplicate]
(8 answers)
Closed 5 years ago.
Although this should be rather easily done with a for loop, I'm wondering wether there is a concise way to delete a set of positions from a Python list. E.g.:
l = ["A","B","C","D","E","F","G"]
pos = [4,6]
# Is there something close to
l.remove(pos)
Best
del is what you are looking for if you have consecutive indices:
From the documentation:
There is a way to remove an item from a list given its index instead of its value: the del statement
Otherwise, you could generate a new list, by filtering out indices, which you aren't interested in
result = [x for i, x in enumerate(l) if i not in pos]
This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 7 years ago.
Is there any difference between these two ways of returning lists?
Initially the list is empty.
my_list = []
method 1:
my_list.append(1)
return my_list
method 2
return my_list.append(1)
Actually, the second method is returning an empty list for me. Please clarify why it is happening like this
When you type the following:
return my_list
You are returning a list object. When you type the following:
return my_list.append(something)
You are returning the result of that method call. In the case of .append() that method is void, so you are effectively returning nothing. If the method .append() appended the argument you pass to it and then returned the modified list itself then you could do it, but that isn't the case.
This question already has answers here:
Accessing the index in 'for' loops
(26 answers)
Closed 6 months ago.
I know that I can set up an integer before the loop and increment it together with the loop. But is there another way?
Just enumerate() the iterable:
for index, obj in enumerate(objects):
print index
(The name object is a built-in type. To avoid shadowing this name, I renamed the loop variable to obj.)
You can use enumerate
for counter, obj in emumerate(objects):
print counter