I want to produce the list: ["*", "*", "*", "*", "*"].
Can this be done in a simple line? (I don't want to have to create a function to do it.)
So far I have been having to do the following, which is very inconvenient and tedious:
v = []
for i in xrange(5):
v.append("*")
v = ('* '*5).split()
result:
Output:
>>> v
['*', '*', '*', '*', '*']
(Nov-21-2017, 10:51 PM)Larz60+ Wrote: [ -> ] v = ('* '*5).split()
result:
Output:
>>> v
['*', '*', '*', '*', '*']
Thanks. How come it doesn't work to then append to v?
So for example:
v = ('* '*5).split()
print v.append("*")
gives an output of
Output:
None
You code as list comprehensions.
>>> v = ['*' for i in range(5)]
>>> v
['*', '*', '*', '*', '*']
Or:
>>> v = ['*'] * 5
>>> v
['*', '*', '*', '*', '*']
I can do it with the one i wrote:
>>> v = ('* '*5).split()
>>> v
['*', '*', '*', '*', '*']
>>> v.append('-')
>>> v
['*', '*', '*', '*', '*', '-']
>>>
list.append() will append the item, but it returns None. Since you're print()
ing the return value of list.append(), that's why you see None.