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

Problem with append

https://code.sololearn.com/c30smJbp4vz3/?ref=app In this code I'm trying to get a list of all ternary numbers (base 3) with a certain length that is passed to the function. When I print the combination I get the right result But when I append them onto a list and return it all combinations (of 0, 1, 2) are all '2'

6th Aug 2018, 12:08 PM
Anton Böhler
Anton Böhler - avatar
4 Answers
+ 1
Does changing lines 10 and 23 to result.append(arr[:]) fix your issue? When you directly append arr to result, whatever changes you make to arr get reflected in result as well. Your last value of arr is ['2', '2'], so result only has that list repeated a number of times. When you do result.append(arr[:]), you are actually appending a (shallow) copy of arr, not the list arr itself. That's why this should do work for your problem. Does that make sense?
6th Aug 2018, 12:18 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
it makes sense thank, you!!
6th Aug 2018, 12:32 PM
Anton Böhler
Anton Böhler - avatar
+ 1
Great! You're most welcome, Anton! Happy coding! 😊
6th Aug 2018, 12:39 PM
Kishalaya Saha
Kishalaya Saha - avatar
0
how can i do a is_palindrome_ stack ( text) function with the stack method ?🤔
25th Nov 2019, 11:37 PM
Sahra.P
Sahra.P - avatar