Sometimes we want a list of zero and there is a huge number of zero then its hard to make it by own then use this methods. In this tutorial we will learn about How to create List of Zeros in Python. So lets learn this by given below methods.
How to create List of Zeros in Python
- Create List of Zeros in Python
to create List of Zeros in Python just Use * operator. By using * operator you can print zero so many times. Here you have to just make a list. Here i have been make a list with mylst and then use * like this way [0]*8 you have to enter a number how many times you want to print a zero then print mylst and you can see your list in your output. Lets see this in below example:
mylst = [0] * 8 print(mylst) mylst = [0] * 7 print(mylst)
Output :[0, 0, 0, 0, 0, 0, 0, 0] [0, 0, 0, 0, 0, 0, 0]
- How to create List of Zeros in Python
to create List of Zeros in Python just Use itertols.repeat(). To print a list of zero you can use itertols.repeat(). By using itertols.repeat() you can print the zero so many times. itertols.repeat() contain two parameters one is zero which you wanting to print in list and the other is how many time you want to print it. Lets learn this by given below example:
import itertools mylst = list(itertools.repeat(0, 8)) print(mylst) import itertools mylst = list(itertools.repeat(0, 7)) print(mylst)
Output :[0, 0, 0, 0, 0, 0, 0, 0] [0, 0, 0, 0, 0, 0, 0]
Method 1: Use * operator
By using * operator you can print zero so many times. Here you have to just make a list. Here i have been make a list with mylst and then use * like this way [0]*8 you have to enter a number how many times you want to print a zero then print mylst and you can see your list in your output. Lets see this in below example:
mylst = [0] * 8
print(mylst)
mylst = [0] * 7
print(mylst)
Output :
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
Method 2: Use itertols.repeat()
To print a list of zero you can use itertols.repeat(). By using itertols.repeat() you can print the zero so many times. itertols.repeat() contain two parameters one is zero which you wanting to print in list and the other is how many time you want to print it. Lets learn this by given below example:
import itertools
mylst = list(itertools.repeat(0, 8))
print(mylst)
import itertools
mylst = list(itertools.repeat(0, 7))
print(mylst)
Output :
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
Method 3: Use for loop
By using for loop you can print a list of zero. You have to just enter both parameter first is zero and the other is how many time you want to print zero and it will give you your list of zero. Lets learn this by an example:
mylst = list(0 for i in range(0, 8))
print(mylst)
mylst = list(0 for i in range(0, 7))
print(mylst)
Output :
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
Method : 4
By using this method you can also print a list of zero. Here we will use a function which will return the list of zero. Lets learn this by given below example:
def make_zeros(number):
return [0] * number
list = make_zeros(8)
print(list)
list = make_zeros(7)
print(list)
Output :
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
Conclusion
So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.