map returning map object and converting it to list removes all elements in it [duplicate]
This question already has an answer here:
Python: calling 'list' on a map object twice
1 answer
Just learned that the object returned from map() doesn't hold up once it has been used in a in expression or it was converted into a list.
What is causing b to get emptied at the end?
>>> a = [1, 2, 3]
>>> b = map(lambda x: x, a)
>>> b
<map object at 0x104d8ccc0>
>>> list(b)
[1, 2, 3]
>>> list(b)
python python-3.x map-function
marked as duplicate by Tomothy32, ShadowRanger, Austin, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 20 at 3:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Python: calling 'list' on a map object twice
1 answer
Just learned that the object returned from map() doesn't hold up once it has been used in a in expression or it was converted into a list.
What is causing b to get emptied at the end?
>>> a = [1, 2, 3]
>>> b = map(lambda x: x, a)
>>> b
<map object at 0x104d8ccc0>
>>> list(b)
[1, 2, 3]
>>> list(b)
python python-3.x map-function
marked as duplicate by Tomothy32, ShadowRanger, Austin, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 20 at 3:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
My bad. Thanks for the englightenment. Btw, isn't it more naturalmapreturning the same collection(type)? Is there any merit in addition to some gain in performance of iteration immediately following it?
– msk
Jan 20 at 4:16
add a comment |
This question already has an answer here:
Python: calling 'list' on a map object twice
1 answer
Just learned that the object returned from map() doesn't hold up once it has been used in a in expression or it was converted into a list.
What is causing b to get emptied at the end?
>>> a = [1, 2, 3]
>>> b = map(lambda x: x, a)
>>> b
<map object at 0x104d8ccc0>
>>> list(b)
[1, 2, 3]
>>> list(b)
python python-3.x map-function
This question already has an answer here:
Python: calling 'list' on a map object twice
1 answer
Just learned that the object returned from map() doesn't hold up once it has been used in a in expression or it was converted into a list.
What is causing b to get emptied at the end?
>>> a = [1, 2, 3]
>>> b = map(lambda x: x, a)
>>> b
<map object at 0x104d8ccc0>
>>> list(b)
[1, 2, 3]
>>> list(b)
This question already has an answer here:
Python: calling 'list' on a map object twice
1 answer
python python-3.x map-function
python python-3.x map-function
edited Jan 20 at 4:18
msk
asked Jan 20 at 3:05
mskmsk
1004
1004
marked as duplicate by Tomothy32, ShadowRanger, Austin, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 20 at 3:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Tomothy32, ShadowRanger, Austin, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 20 at 3:20
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
My bad. Thanks for the englightenment. Btw, isn't it more naturalmapreturning the same collection(type)? Is there any merit in addition to some gain in performance of iteration immediately following it?
– msk
Jan 20 at 4:16
add a comment |
My bad. Thanks for the englightenment. Btw, isn't it more naturalmapreturning the same collection(type)? Is there any merit in addition to some gain in performance of iteration immediately following it?
– msk
Jan 20 at 4:16
My bad. Thanks for the englightenment. Btw, isn't it more natural
map returning the same collection(type)? Is there any merit in addition to some gain in performance of iteration immediately following it?– msk
Jan 20 at 4:16
My bad. Thanks for the englightenment. Btw, isn't it more natural
map returning the same collection(type)? Is there any merit in addition to some gain in performance of iteration immediately following it?– msk
Jan 20 at 4:16
add a comment |
3 Answers
3
active
oldest
votes
map outputs an iterator that applies a function (lambda x: x) over some iterators (a). As a result, b is an iterator. When calling list(b) for the first time, the iterator b is called several times until it reaches to its end. Afterward, b is an iterator which does not have any item left to produce. That's why when you call list(b) for the second time, it outputs an empty list.
1
OK. it isiteratornot iterable as I expected. Though not sure about its design idea, it is just as it is. Thanks.
– msk
Jan 20 at 4:06
add a comment |
The documentation for map specifies that it returns an "iterator", not an "iterable". Python defines an iterator to loop exactly once with no repetition; once the end is reached then it will never return another item.
The second execution of list(b) attempts to build a list from an iterator that is already at the end, so it returns no items and an empty list is constructed.
add a comment |
Whenever you are calling list(b) you are reassigning a new list to that variable b if you want to store b as a list use
a = [1, 2, 3]
b = list(map(lambda x: x, a))
print(b)
>>> [1, 2, 3]
print(*b)
>>> 1 2 3
Hope this helped.
Callinglist(b)doesn't assign to anything.
– wjandrea
Jan 20 at 3:23
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
map outputs an iterator that applies a function (lambda x: x) over some iterators (a). As a result, b is an iterator. When calling list(b) for the first time, the iterator b is called several times until it reaches to its end. Afterward, b is an iterator which does not have any item left to produce. That's why when you call list(b) for the second time, it outputs an empty list.
1
OK. it isiteratornot iterable as I expected. Though not sure about its design idea, it is just as it is. Thanks.
– msk
Jan 20 at 4:06
add a comment |
map outputs an iterator that applies a function (lambda x: x) over some iterators (a). As a result, b is an iterator. When calling list(b) for the first time, the iterator b is called several times until it reaches to its end. Afterward, b is an iterator which does not have any item left to produce. That's why when you call list(b) for the second time, it outputs an empty list.
1
OK. it isiteratornot iterable as I expected. Though not sure about its design idea, it is just as it is. Thanks.
– msk
Jan 20 at 4:06
add a comment |
map outputs an iterator that applies a function (lambda x: x) over some iterators (a). As a result, b is an iterator. When calling list(b) for the first time, the iterator b is called several times until it reaches to its end. Afterward, b is an iterator which does not have any item left to produce. That's why when you call list(b) for the second time, it outputs an empty list.
map outputs an iterator that applies a function (lambda x: x) over some iterators (a). As a result, b is an iterator. When calling list(b) for the first time, the iterator b is called several times until it reaches to its end. Afterward, b is an iterator which does not have any item left to produce. That's why when you call list(b) for the second time, it outputs an empty list.
edited Jan 20 at 3:22
wjandrea
1,0771328
1,0771328
answered Jan 20 at 3:17
noidsiriusnoidsirius
1197
1197
1
OK. it isiteratornot iterable as I expected. Though not sure about its design idea, it is just as it is. Thanks.
– msk
Jan 20 at 4:06
add a comment |
1
OK. it isiteratornot iterable as I expected. Though not sure about its design idea, it is just as it is. Thanks.
– msk
Jan 20 at 4:06
1
1
OK. it is
iterator not iterable as I expected. Though not sure about its design idea, it is just as it is. Thanks.– msk
Jan 20 at 4:06
OK. it is
iterator not iterable as I expected. Though not sure about its design idea, it is just as it is. Thanks.– msk
Jan 20 at 4:06
add a comment |
The documentation for map specifies that it returns an "iterator", not an "iterable". Python defines an iterator to loop exactly once with no repetition; once the end is reached then it will never return another item.
The second execution of list(b) attempts to build a list from an iterator that is already at the end, so it returns no items and an empty list is constructed.
add a comment |
The documentation for map specifies that it returns an "iterator", not an "iterable". Python defines an iterator to loop exactly once with no repetition; once the end is reached then it will never return another item.
The second execution of list(b) attempts to build a list from an iterator that is already at the end, so it returns no items and an empty list is constructed.
add a comment |
The documentation for map specifies that it returns an "iterator", not an "iterable". Python defines an iterator to loop exactly once with no repetition; once the end is reached then it will never return another item.
The second execution of list(b) attempts to build a list from an iterator that is already at the end, so it returns no items and an empty list is constructed.
The documentation for map specifies that it returns an "iterator", not an "iterable". Python defines an iterator to loop exactly once with no repetition; once the end is reached then it will never return another item.
The second execution of list(b) attempts to build a list from an iterator that is already at the end, so it returns no items and an empty list is constructed.
answered Jan 20 at 3:18
lehiesterlehiester
472211
472211
add a comment |
add a comment |
Whenever you are calling list(b) you are reassigning a new list to that variable b if you want to store b as a list use
a = [1, 2, 3]
b = list(map(lambda x: x, a))
print(b)
>>> [1, 2, 3]
print(*b)
>>> 1 2 3
Hope this helped.
Callinglist(b)doesn't assign to anything.
– wjandrea
Jan 20 at 3:23
add a comment |
Whenever you are calling list(b) you are reassigning a new list to that variable b if you want to store b as a list use
a = [1, 2, 3]
b = list(map(lambda x: x, a))
print(b)
>>> [1, 2, 3]
print(*b)
>>> 1 2 3
Hope this helped.
Callinglist(b)doesn't assign to anything.
– wjandrea
Jan 20 at 3:23
add a comment |
Whenever you are calling list(b) you are reassigning a new list to that variable b if you want to store b as a list use
a = [1, 2, 3]
b = list(map(lambda x: x, a))
print(b)
>>> [1, 2, 3]
print(*b)
>>> 1 2 3
Hope this helped.
Whenever you are calling list(b) you are reassigning a new list to that variable b if you want to store b as a list use
a = [1, 2, 3]
b = list(map(lambda x: x, a))
print(b)
>>> [1, 2, 3]
print(*b)
>>> 1 2 3
Hope this helped.
answered Jan 20 at 3:14
Apex PredatorApex Predator
24
24
Callinglist(b)doesn't assign to anything.
– wjandrea
Jan 20 at 3:23
add a comment |
Callinglist(b)doesn't assign to anything.
– wjandrea
Jan 20 at 3:23
Calling
list(b) doesn't assign to anything.– wjandrea
Jan 20 at 3:23
Calling
list(b) doesn't assign to anything.– wjandrea
Jan 20 at 3:23
add a comment |
My bad. Thanks for the englightenment. Btw, isn't it more natural
mapreturning the same collection(type)? Is there any merit in addition to some gain in performance of iteration immediately following it?– msk
Jan 20 at 4:16