Help with adding to an empty list

Reply

Join Date: Oct 2009
Posts: 2
Reputation: jsw24 is an unknown quantity at this point 
Solved Threads: 0
jsw24 jsw24 is offline Offline
Newbie Poster

Help with adding to an empty list

 
0
  #1
Oct 19th, 2009
I am trying to add to an empty list in python and I am having
trouble. Ill give an example of the code to see if anyone can help
me out....

num = 0
mylist = []

while num < 10:
num = num + 1
mylist = mylist + num

for item in list:
print item
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,099
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 287
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
0
  #2
Oct 19th, 2009
To do it the way you're trying to do it, use this:
  1. >>> num = 0
  2. >>> mylist = []
  3. >>>
  4. >>> while num < 10:
  5. ... num += 1
  6. ... mylist += [num]
  7. ...
  8. >>> for item in mylist:
  9. ... print item
  10. ...
  11. 1
  12. 2
  13. 3
  14. 4
  15. 5
  16. 6
  17. 7
  18. 8
  19. 9
  20. 10
  21. >>>
Notice I put brackets around num since you have to add a list to a list when using the + operator. Other methods would be to use mylist.append(num) or mylist.insert(index, num) . I also changed your x = x + y statements to x += y , which is identical.

HTH
Last edited by jlm699; Oct 19th, 2009 at 5:56 pm.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,243
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 146
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso
 
0
  #3
Oct 19th, 2009
Some corrections:
  1. num = 0
  2. mylist = []
  3.  
  4. while num < 10:
  5. num = num + 1
  6. mylist.append(num)
  7.  
  8. print(mylist)
  9.  
  10. for item in mylist:
  11. print(item)
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 136
Reputation: lrh9 is an unknown quantity at this point 
Solved Threads: 14
lrh9 lrh9 is offline Offline
Junior Poster
 
0
  #4
Oct 19th, 2009
So what you basically want is to fill the list with a sequence of numbers? Why?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: jsw24 is an unknown quantity at this point 
Solved Threads: 0
jsw24 jsw24 is offline Offline
Newbie Poster
 
0
  #5
Oct 19th, 2009
I'm not getting it to print anything! I'll post the exact code I am working with... I'm trying to convert from deci to binary and print the binary number using the list method.

remain = 0
quo = 1
mynum = []

deci = int(raw_input("Please enter a decimal integer:"))

while quo != 0:

quo = deci / 2

remain = deci / 2

mynum.append(remain)

print(mylist)

for item in mynum:
print item
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 243
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 70
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Posting Whiz in Training
 
0
  #6
Oct 19th, 2009
"I'm not getting it to print anything! I'll post the exact code I am working with... I'm trying to convert from deci to binary and print the binary number using the list method."
...
so you're trying to convert a number from decimal to binary??
here's a hint how to do that

number = 14
14 / 2 = 7, remainder = 0
7 / 2 = 3, remainder = 1
3 / 2 = 1, remainder = 1
1 / 2 = 0, remainder = 1

therefore 14 in binary is 1110
you can use l.insert( 0, num ) to deal with the reverse order, where l is a list

hope that helps!
Last edited by masterofpuppets; Oct 19th, 2009 at 8:05 pm.
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Reply

Tags
list

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 831 | Replies: 5
Thread Tools Search this Thread



Tag cloud for list
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC