Need clarification with my for loop logic in Python -


the output should have been [2, 18, 6, 16, 10, 14].

my_list = [1, 9, 3, 8, 5, 7]  number in my_list:     number = 2 * number  print my_list      

the problem prints same my_list values. logic number = 2 * number isn't executed?

you not updating list, updating number variable:

for number in my_list:     number = 2 * number 

there may way this:

using enumerate:

my_list = [1, 9, 3, 8, 5, 7]  index,number in enumerate(my_list):     my_list[index] = 2 * number  print my_list      

using list comprehension:

my_list = [2*x x in my_list] 

using lambda , map:

my_list = map(lambda x: 2*x, my_list) 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -