Python Forum
append not working as expected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
append not working as expected
#1
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


Messages In This Thread
append not working as expected - by teachinggeek - Apr-09-2020, 03:10 AM
RE: append not working as expected - by michael1789 - Apr-09-2020, 03:19 AM
RE: append not working as expected - by buran - Apr-09-2020, 04:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple conditional not working as expected return2sender 8 1,007 Aug-27-2023, 10:39 PM
Last Post: return2sender
  Custom method to handle exceptions not working as expected gradlon93 3 1,028 Dec-22-2022, 07:12 PM
Last Post: deanhystad
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,083 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  set and sorted, not working how expected! wtr 2 1,285 Jan-07-2022, 04:53 PM
Last Post: bowlofred
  Append not working WiPi 3 5,779 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 2,588 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  append no working pythonduffer 3 2,764 Apr-12-2019, 06:32 AM
Last Post: perfringo
  Timer class not working as expected. MuntyScruntfundle 4 2,648 Feb-02-2019, 09:47 AM
Last Post: MuntyScruntfundle
  Code not working as expected. an_droid 4 3,967 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