user8014932
user8014932

Reputation:

code snippet list python easiert form

How do I write this code snippets easier or in one line?

dist=[]
for k in range(5):
    dist.append(k)
dist[0]="Apple"
print(dist)

Upvotes: 0

Views: 41

Answers (2)

galfisher
galfisher

Reputation: 1122

Python 2.7 (works in Python 3+ as well):

>>> dist = ['Apple'] + [i for i in range(1,5)]
>>> dist
['Apple', 1, 2, 3, 4]

Upvotes: 1

Uriel
Uriel

Reputation: 16214

>>> dist = ['Apple', *range(1, 5)]
>>> dist
['Apple', 1, 2, 3, 4]

Upvotes: 5

Related Questions