944,086 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 2296
  • Python RSS
Oct 19th, 2009
0

Help with adding to an empty list

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jsw24 is offline Offline
2 posts
since Oct 2009
Oct 19th, 2009
0
Re: Help with adding to an empty list
To do it the way you're trying to do it, use this:
python Syntax (Toggle Plain Text)
  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 6:56 pm.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Oct 19th, 2009
0
Re: Help with adding to an empty list
Some corrections:
python Syntax (Toggle Plain Text)
  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)
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Oct 19th, 2009
0
Re: Help with adding to an empty list
So what you basically want is to fill the list with a sequence of numbers? Why?
Reputation Points: 106
Solved Threads: 35
Posting Whiz in Training
lrh9 is offline Offline
238 posts
since Oct 2009
Oct 19th, 2009
0
Re: Help with adding to an empty list
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jsw24 is offline Offline
2 posts
since Oct 2009
Oct 19th, 2009
0
Re: Help with adding to an empty list
"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 9:05 pm.
Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: ??OOP??
Next Thread in Python Forum Timeline: pygtk + py2exe + inno -> icon for shortcut





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC