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


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 994 Aug-27-2023, 10:39 PM
Last Post: return2sender
  Custom method to handle exceptions not working as expected gradlon93 3 1,018 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,072 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  set and sorted, not working how expected! wtr 2 1,281 Jan-07-2022, 04:53 PM
Last Post: bowlofred
  Append not working WiPi 3 5,748 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,570 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  append no working pythonduffer 3 2,748 Apr-12-2019, 06:32 AM
Last Post: perfringo
  Timer class not working as expected. MuntyScruntfundle 4 2,637 Feb-02-2019, 09:47 AM
Last Post: MuntyScruntfundle
  Code not working as expected. an_droid 4 3,954 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