Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list trouble
#1
hello, im new to python and having some trouble with lists. im trying to change all the values in a list to 0 but when tring the range function it only goes so far in my list i previously appended it to go up by 1 after each value and now after trying t turn it into zero it gives me
[0,0,0,0,0,6,7,8,9,10]
this is what my code looks like right now:
for i in range (15-6 + 2):
      out.append(i)
for i in range(out[1],out[length]):
      out[i] = 0
any suggestions would be greatly appreciated
Reply
#2
Your code as shown doesn't run, so I don't know how you're getting the data.

What is out[length] supposed to be? Do you have a separate length variable somewhere, or are you trying to get the length of your out list?
Reply
#3
Alternatively you can just multiple [0] with length of the list.
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
#4
(Oct-16-2020, 10:41 PM)rediska Wrote: im trying to change all the values in a list to 0
Why not do the following:
my_list = [0,0,0,0,0,6,7,8,9,10]

print(my_list)

for i in range (len(my_list)):
      my_list[i] = 0

print(my_list)
Output:
[0, 0, 0, 0, 0, 6, 7, 8, 9, 10] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  trouble reading string/module from excel as a list popular_dog 0 386 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  Appending list Trouble Big Time Milfredo 7 3,026 Oct-01-2020, 02:59 AM
Last Post: Milfredo
  trouble with list array Milfredo 2 1,990 Sep-16-2020, 12:07 AM
Last Post: Milfredo
  Trouble with converting list , dict to int values! faryad13 7 3,671 Sep-04-2020, 06:25 AM
Last Post: faryad13
  Trouble with list function Eggman72 2 1,712 Mar-23-2020, 09:36 PM
Last Post: Eggman72
  For loop (adding strings to a list trouble) eoins 2 3,000 Jul-03-2018, 10:36 AM
Last Post: volcano63
  trouble while appending list in for loop py_learner 1 2,815 May-28-2018, 01:55 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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