代码片段列表python easiert表单

时间:2017-05-15 14:58:02

标签: python list python-3.x

如何更容易地或在一行中编写此代码段?

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

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:1)

Python 2.7(也适用于Python 3+):

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