I want to take 1 value out of 10 out of my list but I also want to take the last one (l[19]
).
Can we do a double range in a loop?
l=[1]*20
for i in range(0,20,10)and range(len(l)-1,len(l)):
print("i ="+str(i),l[i])
Or some other way without a double range?
l=[1]*20
for i in range(0,20,10):
print("i = "+str(i), l[i])
Expected: "i = 0, 1", "i = 10, 1", "i = 19, 1"
Actual result: "i = 19, 1",
Answer for python 2 :
How can I add non-sequential numbers to a range?
Best (in my humble opinion) answer for my actual python version(python 3.7) : (because we don't need to import a library or creat a list)
for i in [*range(0, len(l), 10)] + [len(l)-1]:
print("i = {}, {}".format(str(i), l[i]))
Thank you all for your answers !