Why emumerated list is empty on 2nd round of print?

2018-09-06 Thread Viet Nguyen via Python-list
>>> numList
[2, 7, 22, 30, 1, 8]

>>> aList = enumerate(numList)

>>> for i,j in aList:print(i,j)

0 2
1 7
2 22
3 30
4 1
5 8

>>> for i,j in aList:print(i,j)

>>> 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why emumerated list is empty on 2nd round of print?

2018-09-06 Thread Viet Nguyen via Python-list
On Thursday, September 6, 2018 at 10:34:19 AM UTC-7, Chris Angelico wrote:
> On Fri, Sep 7, 2018 at 3:26 AM, Viet Nguyen via Python-list
>  wrote:
> >>>> numList
> > [2, 7, 22, 30, 1, 8]
> >
> >>>> aList = enumerate(numList)
> >
> >>>> for i,j in aList:print(i,j)
> >
> > 0 2
> > 1 7
> > 2 22
> > 3 30
> > 4 1
> > 5 8
> >
> >>>> for i,j in aList:print(i,j)
> >
> >>>>
> 
> Because it's not an enumerated list, it's an enumerated iterator.
> Generally, you'll just use that directly in the loop:
> 
> for i, value in enumerate(numbers):
> 
> There's generally no need to hang onto it from one loop to another.
> 
> ChrisA

Thanks ChrisA. If I do this "aList = enumerate(numList)", isn't it stored 
permanently in aList now?  I see your point to use it directly, but just in 
case I do need to hang onto it from one loop to another, then how is that done? 
  Anyway I think I'm ok and I got what I need for now.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why emumerated list is empty on 2nd round of print?

2018-09-06 Thread Viet Nguyen via Python-list
On Thursday, September 6, 2018 at 12:12:20 PM UTC-7, David Raymond wrote:
> The actual "enumerate" object is really just holding a current index and a 
> reference to the original list. So if you alter the original list while 
> you're iterating through it you'll see the changes. If you want a full copy 
> then you can just wrap it with list()
> 
> Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit 
> (AMD64)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> numList = [2, 7, 22, 30, 1, 8]
> >>> aList = enumerate(numList)
> >>> aList.__next__()
> (0, 2)
> >>> numList[1] = 5
> >>> aList.__next__()
> (1, 5)
> >>> aList2 = list(enumerate(numList))
> >>> aList2
> [(0, 2), (1, 5), (2, 22), (3, 30), (4, 1), (5, 8)]
> >>> numList[3] = -12
> >>> aList2
> [(0, 2), (1, 5), (2, 22), (3, 30), (4, 1), (5, 8)]
> >>> aList.__next__()
> (2, 22)
> >>> aList.__next__()
> (3, -12)
> >>> aList.__next__()
> (4, 1)
> >>> aList.__next__()
> (5, 8)
> >>> aList.__next__()
> Traceback (most recent call last):
>   File "", line 1, in 
> StopIteration
> >>>
> 
> 
> -Original Message-
> From: Python-list 
> [mailto:python-list-bounces+david.raymond=tomtom....@python.org] On Behalf Of 
> Viet Nguyen via Python-list
> Sent: Thursday, September 06, 2018 2:50 PM
> To: python-list@python.org
> Subject: Re: Why emumerated list is empty on 2nd round of print?
> 
> On Thursday, September 6, 2018 at 10:34:19 AM UTC-7, Chris Angelico wrote:
> > On Fri, Sep 7, 2018 at 3:26 AM, Viet Nguyen via Python-list
> >  wrote:
> > >>>> numList
> > > [2, 7, 22, 30, 1, 8]
> > >
> > >>>> aList = enumerate(numList)
> > >
> > >>>> for i,j in aList:print(i,j)
> > >
> > > 0 2
> > > 1 7
> > > 2 22
> > > 3 30
> > > 4 1
> > > 5 8
> > >
> > >>>> for i,j in aList:print(i,j)
> > >
> > >>>>
> > 
> > Because it's not an enumerated list, it's an enumerated iterator.
> > Generally, you'll just use that directly in the loop:
> > 
> > for i, value in enumerate(numbers):
> > 
> > There's generally no need to hang onto it from one loop to another.
> > 
> > ChrisA
> 
> Thanks ChrisA. If I do this "aList = enumerate(numList)", isn't it stored 
> permanently in aList now?  I see your point to use it directly, but just in 
> case I do need to hang onto it from one loop to another, then how is that 
> done?   Anyway I think I'm ok and I got what I need for now.
> -- 
> https://mail.python.org/mailman/listinfo/python-list

Very clear and good examples!  Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list


What is not working with my "map" usage?

2018-09-21 Thread Viet Nguyen via Python-list
Hi,

I want to add up all of the list elements.  But when I use the "map" function, 
it didn't seem to work as I expect.  Could someone point out how "map" can be 
applied here then?

def add_all_elements (*args):  
total = 0
for i in args:
   print(type(i))
   print("i = %s" % i)
   print("BEFORE total = %s" % total)
   total += int(i)
   print("AFTER total = %s\n" % total)
print("FINAL total = %s\n" % total)
return total


alist = ['2', '09', '49']


## this one works Okay

add_all_elements(*alist)

i = 2
BEFORE total = 0
AFTER total = 2


i = 09
BEFORE total = 2
AFTER total = 11


i = 49
BEFORE total = 11
AFTER total = 60

FINAL total = 60


## Why is this NOT Okay when I use map ??  What must I change ?

>>> list(map(add_all_elements,alist))

i = 2
BEFORE total = 0
AFTER total = 2

FINAL total = 2


i = 09
BEFORE total = 0
AFTER total = 9

FINAL total = 9


i = 49
BEFORE total = 0
AFTER total = 49

FINAL total = 49

[2, 9, 49]


Thanks,
Viet
-- 
https://mail.python.org/mailman/listinfo/python-list