By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,495 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

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

P: 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
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 624
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.