##### append not working as expected
 append not working as expected teachinggeek Unladen Swallow Posts: 1 Threads: 1 Joined: Apr 2020 Reputation: Apr-09-2020, 03:10 AM So I am trying to create a list (called megalist) of all of the 3 digit combinations from the list of numbers called numbers. I think the logic is sound (I am testing the process with print commands and they seem to suggest all is well.) But for some reason the append to is not simply appending. Each time a new 3-digit combo is created and added to megalist it replaces the previous combos collected with the newest combo as well. I can't figure out why! So, megalist begins empty. First combo is created [1,3,5] megalist becomes [[1,3,5]] next combo is created [1,3,7] ```numbers=[1,3,5,7,9,11,13,15] megalist=[] list_of_3=[-1,-1,-1] for first in range(len(numbers)-1): #selecting first number from the 1 till 11 first_num=numbers[first] list_of_3[0]=first_num print('list of 3 is ', list_of_3) for second in range(1+numbers.index(list_of_3[0]),len(numbers)): #selecting second 3 to 13 second_num = numbers[second] list_of_3[1]=second_num print('list of 3 is ', list_of_3) for third in range(1+numbers.index(list_of_3[1]),len(numbers)+1): #selecting 3rd 5 to 15 third_num = numbers[third] print('3rd number is ', third_num) list_of_3[2]=third_num print('list of 3 is ', list_of_3) megalist.insert(0,list_of_3) print('combos = ',megalist)``` Reply michael1789 Minister of Silly Walks Posts: 419 Threads: 34 Joined: May 2019 Reputation: Apr-09-2020, 03:19 AM You aren't appending, you are redefining things. what you want it is `megalist.append(list_of_three)`This will tack it onto the end. Reply Posts: 7,614 Threads: 133 Joined: Sep 2016 Reputation: Apr-09-2020, 04:32 AM (This post was last modified: Apr-09-2020, 04:34 AM by buran.) (Apr-09-2020, 03:19 AM)michael1789 Wrote: You aren't appending, you are redefining things. what you want it is `megalist.append(list_of_three)`This will tack it onto the end.They insert the list always in position 0. Technically not appending, you are right, but that is not the cause of the problem. Even if they append, the problem will persist. @teachinggeek: Lists are mutable object. Basically you insert the same object over and over again. Changes that you make affect also elements already inserted in the final list. That said, it's better to look at itertools.combinations() ```>>> import itertools >>> numbers=[1, 3, 5, 7, 9, 11, 13, 15] >>> list(itertools.combinations(numbers, 3)) [(1, 3, 5), (1, 3, 7), (1, 3, 9), (1, 3, 11), (1, 3, 13), (1, 3, 15), (1, 5, 7), (1, 5, 9), (1, 5, 11), (1, 5, 13), (1, 5, 15), (1, 7, 9), (1, 7, 11), (1, 7, 13), (1, 7, 15), (1, 9, 11), (1, 9, 13), (1, 9, 15), (1, 11, 13), (1, 11, 15), (1, 13, 15), (3, 5, 7), (3, 5, 9), (3, 5, 11), (3, 5, 13), (3, 5, 15), (3, 7, 9), (3, 7, 11), (3, 7, 13), (3, 7, 15), (3, 9, 11), (3, 9, 13), (3, 9, 15), (3, 11, 13), (3, 11, 15), (3, 13, 15), (5, 7, 9), (5, 7, 11), (5, 7, 13), (5, 7, 15), (5, 9, 11), (5, 9, 13), (5, 9, 15), (5, 11, 13), (5, 11, 15), (5, 13, 15), (7, 9, 11), (7, 9, 13), (7, 9, 15), (7, 11, 13), (7, 11, 15), (7, 13, 15), (9, 11, 13), (9, 11, 15), (9, 13, 15), (11, 13, 15)]``` If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Append not working WiPi 3 1,805 May-05-2020, 03:31 PM Last Post: deanhystad Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 891 Apr-22-2020, 01:01 PM Last Post: deanhystad append no working pythonduffer 3 1,360 Apr-12-2019, 06:32 AM Last Post: perfringo Timer class not working as expected. MuntyScruntfundle 4 1,221 Feb-02-2019, 09:47 AM Last Post: MuntyScruntfundle Code not working as expected. an_droid 4 2,699 Mar-09-2017, 02:14 PM Last Post: an_droid

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020