Problem with list operation / function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Problem with list operation / function

Can anybody tell me why after 'wow' function is executed on 'a' list, the original list is not changed,but after 'nice' function executed on 'b' list, the original list is changed? https://code.sololearn.com/c7eUYVGiq4Zk/?ref=app

18th Jul 2020, 8:31 AM
Mir Abir Hossain
Mir Abir Hossain - avatar
5 Answers
+ 3
1st case : a new list is created from an iterator (filter) on the list given into parameter. This iterator doesn't modify the original list. 2nde case : the '+=' operator. On lists, this operator behaves in the same way as the '.append' method : it modifies directly the list. Since it's confusing, please use 'append' method over '+=' operator.
18th Jul 2020, 9:45 AM
Thรฉophile
Thรฉophile - avatar
+ 2
Mir Abir Hossain its because filter function generates new list based on the function you apply and original list doesn't change. And in 2nd case you added 2 to every value in list or you have changed the values in the list. So, the 2nd list change and 1st didn't.
18th Jul 2020, 8:36 AM
AKSHAY๐Ÿ‡ฎ๐Ÿ‡ณ
AKSHAY๐Ÿ‡ฎ๐Ÿ‡ณ - avatar
18th Jul 2020, 2:54 PM
Mir Abir Hossain
Mir Abir Hossain - avatar
+ 1
Thanks AKSHAY , for the answer
18th Jul 2020, 9:05 AM
Mir Abir Hossain
Mir Abir Hossain - avatar