What are the various ways in which we can create a list containing 50 one's using python?
Asked
Active
Viewed 837 times
-7
-
My favourite way: `from collections import OrderedDict; exec("".join(map(chr,[int("".join(str(OrderedDict([(':)', 0), (':D', 1), (':P', 2), (':S', 3), (':(', 4), ('=)', 5), ('=/', 6), (':/', 7), (':{', 8), (';)', 9)])[i]) for i in x.split())) for x in ":D :D :P :D :D :( :D :) =) :D :D :) :D :D =/ :( :) ;) :D :( ;) \ ;) :S :( :P =) :S :( :{ :( :D :D :)" .split(" ")])))` https://pastebin.com/YWNiV2Fy – Chris_Rands Sep 21 '18 at 13:55
2 Answers
3
>>> a = [1]*50
>>> a
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
>>>
>>> a = [1 for _ in range(50)]
>>> a
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
>>>

Nouman
- 6,947
- 7
- 32
- 60
-3
Here are hundreds of ways to do this. I think one of the simplest ones is using list comprehension
, like:
ones = [1 for i in range(50)]

Taohidul Islam
- 5,246
- 3
- 26
- 39