map returning map object and converting it to list removes all elements in it [duplicate]












-1
















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)










share|improve this question















marked as duplicate by Tomothy32, ShadowRanger, Austin, jpp python-3.x
Users with the  python-3.x badge can single-handedly close python-3.x questions as duplicates and reopen them as needed.

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 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
















-1
















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)










share|improve this question















marked as duplicate by Tomothy32, ShadowRanger, Austin, jpp python-3.x
Users with the  python-3.x badge can single-handedly close python-3.x questions as duplicates and reopen them as needed.

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 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














-1












-1








-1









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)










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 at 4:18







msk

















asked Jan 20 at 3:05









mskmsk

1004




1004




marked as duplicate by Tomothy32, ShadowRanger, Austin, jpp python-3.x
Users with the  python-3.x badge can single-handedly close python-3.x questions as duplicates and reopen them as needed.

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 python-3.x
Users with the  python-3.x badge can single-handedly close python-3.x questions as duplicates and reopen them as needed.

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 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

















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












3 Answers
3






active

oldest

votes


















3














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.






share|improve this answer





















  • 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



















1














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.






share|improve this answer































    -2














    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.






    share|improve this answer
























    • Calling list(b) doesn't assign to anything.

      – wjandrea
      Jan 20 at 3:23


















    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    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.






    share|improve this answer





















    • 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
















    3














    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.






    share|improve this answer





















    • 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














    3












    3








    3







    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 20 at 3:22









    wjandrea

    1,0771328




    1,0771328










    answered Jan 20 at 3:17









    noidsiriusnoidsirius

    1197




    1197








    • 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














    • 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








    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













    1














    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.






    share|improve this answer




























      1














      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.






      share|improve this answer


























        1












        1








        1







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 20 at 3:18









        lehiesterlehiester

        472211




        472211























            -2














            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.






            share|improve this answer
























            • Calling list(b) doesn't assign to anything.

              – wjandrea
              Jan 20 at 3:23
















            -2














            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.






            share|improve this answer
























            • Calling list(b) doesn't assign to anything.

              – wjandrea
              Jan 20 at 3:23














            -2












            -2








            -2







            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.






            share|improve this answer













            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 20 at 3:14









            Apex PredatorApex Predator

            24




            24













            • 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

















            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



            Popular posts from this blog

            Callistus III

            Ostreoida

            Plistias Cous