Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing a large list of booleans in Python [duplicate]

Possible Duplicate:
Initialize list with same bool value

I'm attempting to make a prime number generator in python 2.7 and plan to use an array (or list) of booleans which will indicate whether a given number is prime.

Let's say I wanted to initialize a list of 5000 booleans, how would I do so without having to manually type [True, True, ...]

like image 660
flau Avatar asked Dec 07 '12 22:12

flau


1 Answers

You could try this:

[True] * 5000

Lists can be multiplied in Python (as can strings):

>>> [True] * 3
[True, True, True]
>>> "abc" * 3
'abcabcabc'
like image 199
arshajii Avatar answered Nov 13 '22 18:11

arshajii