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 5 years ago.
The outcome is None with list(a) the second time. Anyone have a clue on that?
>>> test = {1: 2, 3: 4}
>>> a= test.iterkeys()
>>> list(a)
**[1, 3]**
>>> list(a)
**[]**
>>> list(a)
[]
iterkeys returns an iterator, which, as any iterator, can be iterated over only once.
list consumes the whole iterator, so that the latter can't provide any more values, so the subsequent lists are empty.
Related
This question already has answers here:
Zipped Python generators with 2nd one being shorter: how to retrieve element that is silently consumed
(8 answers)
Closed 1 year ago.
just out of curiosity I wanted to ask, why is the following wanted behavior in Python?
Suppose we have:
a = [1,2,3]
b = [1,2,3]
c = [1,2,3,4]
ai = iter(a)
bi = iter(b)
ci = iter(c)
Now, I expected the following example to print [4], however, it prints [].
for _ in zip(ci, zip(ai, bi)):
pass
print(list(ci))
Console:
>>> print(list(ci))
[]
But when I change arguments of the outter zip, it prints what I expect ([4]).
for _ in zip(zip(ai, bi), ci):
pass
print(list(ci))
Console:
>>> print(list(ci))
[4]
Shouldn't both example print the same result? Thanks.
next() of a zip iterator will try to read the next element of each of its argument iterators in order. If any of them reports that it's exhausted, the zip iterator will stop and report that it's exhausted.
This means that it will perform an extra iteration on all the initial arguments that are longer than the shortest one. This means that it reads an extra element of c.
You can see this ordering and extra iteration in the equivalent code in the documentation.
Change c to [1, 2, 3, 4, 5] and you'll get [5] at the end.
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 5 years ago.
guys!
I'm using Python 3.6.1 and got into an interesting issue:
I'm using a simple itertools.filter() to get a sub-list from an items list, and then I just print it twice.
items = [1,2,3,4,5]
operateVersions = filter(lambda t: t, items)
print ("1: %s" % list(operateVersions))
print ("2: %s" % list(operateVersions))
The result is weird:
1: [1, 2, 3, 4, 5]
2: []
so, when i run list(operateVersions) it somehow rewrites operateVersions filter object instead of just returning the list interpretation
Is it an OK behavior? It doesn't look for me it is
A filter is a special iterable object, and like a generator, you can only iterate over it once. So essentially it returns an empty list when you run it a second time.
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 5 years ago.
aTup = (1,2,3)
print(list(aTup).append(4))
Why does it display None?
append returns None, simple as that. It does however modify the list
>>> l = [1,2,3]
>>> print(l.append(4))
None
>>> l
[1, 2, 3, 4]
The reason is that it isn't meant to be called with the return value used mistakenly for an assignment.
l = l.append(4) # this is wrong
It's because the method append() does not return any value.
If you want to print the list after the update, you can do the following:
aTup = (1,2,3)
aList = list(aTup)
aList.append(4)
print(aList)
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 6 years ago.
What is the actual difference between list1.append() and list1+list2 in python??
Along with this, why the following statement return NULL?
print(list1.append(list2))
{where list1 and list2 are 2 simple list}
list.append() modifies the object and returns None.
[] + [] creates and "returns" new list.
https://docs.python.org/2.7/tutorial/datastructures.html#more-on-lists
Recommended reading: http://docs.python-guide.org/en/latest/writing/gotchas/
Returning None is a way to communicate that an operation is side-effecting -- that is, that it's changing one of its operands, as opposed to leaving them unchanged and returning a new value.
list1.append(list2)
...changes list1, making it a member of this category.
Compare the following two chunks of code:
# in this case, it's obvious that list1 was changed
list1.append(list2)
print list1
...and:
# in this case, you as a reader don't know if list1 was changed,
# unless you already know the semantics of list.append.
print list1.append(list2)
Forbidding the latter (by making it useless) thus enhances the readability of the language.
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> a.append(b) # append just appends the variable to the next index and returns None
>>> print a
[1,2,3,[4,5,6]]
>>> a.extend(b) # Extend takes a list as input and extends the main list
[1,2,3,4,5,6]
>>> a+b # + is exactly same as extend
[1,2,3,4,5,6]
When you print a function, you print what it returns and the append method does not return anything. However your list now has a new element. You can print the list to see the changes made.
list1 + list2 means that you combine 2 lists into one big list.
list1.append(element) adds one element to the end of the list.
Here's an example of append vs +
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> a + b
[1, 2, 3, 4, 5, 6]
>>>
>>> a.append(b)
>>> a
[1, 2, 3, [4, 5, 6]]
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
I'm trying to understand the difference between the two methods below:
def first_append(new_item, a_list=[]):
a_list.append(new_item)
return a_list
def second_append(new_item, a_list=None):
if a_list is None:
a_list = []
a_list.append(new_item)
return a_list
first_append keeps adding to a_list when called multiple times, causing it to grow. However, second_append always returns a list of length 1. What is the difference here?
Examples:
>>> first_append('one')
['one']
>>> first_append('two')
['one', 'two']
>>> second_append('one')
['one']
>>> second_append('two')
['two']
Function second_append always creates a new local list for you each time it is called.