469,309 Members | 2,786 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,309 developers. It's quick & easy.

How do I make a python list with 7 (or any number) empty elements?

3
I am trying to make an empty list with a set number of elements.

I know how to make an empty list.
Expand|Select|Wrap|Line Numbers
  1. x = []
I also know how to make a normal list.
Expand|Select|Wrap|Line Numbers
  1. x = ["d","x","f"]
I am trying to do the following.
Expand|Select|Wrap|Line Numbers
  1.  
  2. #set amount of elements
  3. SIZE = 7
  4.  
  5. array = [SIZE]
  6.  
I want the array variable to hold 7 empty elements, not the value 7. Can anyone help?

UPDATE***

I figured it out.

I wanted to make a program that lets the user enter the total sales for the day, and then my program would output the sum of the sales for the week.

Here is my code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. def main():
  3.  
  4.     total = 0.0
  5.     SIZE = 7
  6.     sales = [0] * SIZE
  7.     index = 0
  8.  
  9.     day = ["Monday","Tuesday","Wednsday","Thursday","Friday","Saturday","Sunday"]
  10.  
  11.     for index in range(SIZE):
  12.  
  13.         print "Enter the amount of sales for", day[index]
  14.         sales[index] = float(raw_input("Enter the sales here: "))
  15.  
  16.         total = total + sales[index]
  17.  
  18.     print "The total sales for the week is $",total 
  19.  
  20. main()
  21.  
  22.  
Jul 10 '11 #1
1 8960
dwblas
626 Expert 512MB
You can append whatever you want to a list, as many times as you want. There is no such thing as an "empty element" however, as it can't be an element if it is empty. You will have to decide what type of element you want, string type, integer type, None type, etc. and append that 7 times using a for loop. Also, your question is too vague to give any kind of a meaningful response. If you state the problem, and post some sample code, you will get more responses.
Jul 11 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

23 posts views Thread by Fuzzyman | last post: by
reply views Thread by Chris | last post: by
11 posts views Thread by Peter Oliphant | last post: by
3 posts views Thread by laredotornado | last post: by
8 posts views Thread by jerry.levan | last post: by
3 posts views Thread by August Karlstrom | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.