Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
append no working
#1
I wrote this simple python program

L=[]

for i in range(0,5):                                        
    L.append(i)  
   
print(*L, sep = ", ")

The output is 0, 1, 2, 3, 4
Why is the output in reverse order?

Thanks
Reply
#2
It is not in reverse order. Append adds items at the end of the list. Is this a homework to print a countdown? You can obtain the reverse order by using proper arguments in the call to range()
Reply
#3
Thanks
It's not a homework problem.
Reply
#4
If you want to retain your current code structure then you can replace append with insert:

>>> lst = list()
>>> for i in range(0,5):
...     lst.insert(0, i)
... 
>>> lst
[4, 3, 2, 1, 0]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Append not working WiPi 3 5,663 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,523 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  append not working as expected teachinggeek 2 2,220 Apr-09-2020, 04:32 AM
Last Post: buran

Forum Jump:

User Panel Messages

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