Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of zeros in python [duplicate]

Tags:

python

How can I create a list which contains only zeros? I want to be able to create a zeros list for each int in range(10)

For example, if the int in the range was 4 I will get:

[0,0,0,0] 

and for 7:

[0,0,0,0,0,0,0] 
like image 776
user1040563 Avatar asked Dec 15 '11 23:12

user1040563


People also ask

How do I make a list in one in Python?

In Python, a list is created by placing elements inside square brackets [] , separated by commas.


1 Answers

#add code here to figure out the number of 0's you need, naming the variable n. listofzeros = [0] * n 

if you prefer to put it in the function, just drop in that code and add return listofzeros

Which would look like this:

def zerolistmaker(n):     listofzeros = [0] * n     return listofzeros 

sample output:

>>> zerolistmaker(4) [0, 0, 0, 0] >>> zerolistmaker(5) [0, 0, 0, 0, 0] >>> zerolistmaker(15) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] >>>  
like image 77
Tiffany Avatar answered Oct 05 '22 20:10

Tiffany