Reputation: 11
I want to print first and last value of list a
in b
. I'm a newbie in python
b = []
a = [5, 10, 15, 20, 25]
for i in a:
if i == 0:
b.append(a[i])
if i == len(a)-1:
b.append(a[i])
Expected Result
print(b)
[5,25]
Upvotes: 1
Views: 102
Reputation: 918
In Python you don't need to be calculating the length of a list. If you want to add the first and the last elements of a list into another, you only have to do this:
a = [5, 10, 15, 20, 25]
b = [a[0],a[-1]]
print(b)
Upvotes: 1