-2

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 !

  • `range(...) and range(...)` will always evaluate to the first range, unless it's empty; `and` returns the first truthy operand. You could make a list of the indices, and add the last index if it's not there already. – jonrsharpe May 21 '19 at 08:03
  • Do you want `i` to iterate the first range, and then the second, or should `i` be tuples of values from the two ranges, and if so, of all combinations, or of the elements that are in the same positions? – tobias_k May 21 '19 at 08:05
  • Yeah it seems like a duplicate, i will link mine to that one or delete it if i can – Matthew Iqbal May 21 '19 at 08:17
  • link I posted (deleted) is for python 2 – Andrew Allen May 21 '19 at 08:19

5 Answers5

2

Use zip:

for x, y in zip(range(10), range(10, 0, -1)):
...     print(x, y)
...
0 10
1 9
2 8
3 7
4 6
5 5
6 4
7 3
8 2
9 1
Netwave
  • 40,134
  • 6
  • 50
  • 93
0

You can concatenate lists with + so:

>>> l=[1]*20
>>> range(0, len(l), 10) + [len(l)-1]
[0, 10, 19]

so:

>>> for i in range(0, len(l), 10) + [len(l)-1]:
    print("i = "+str(i), l[i])

('i = 0', 1)
('i = 10', 1)
('i = 19', 1)

That gives exactly the output you are asking for, but I suspect there's some subtlety of what you actually want that I'm missing.

Simon Hibbs
  • 5,941
  • 5
  • 26
  • 32
  • I get `TypeError: unsupported operand type(s) for +: 'range' and 'list'`. Are you using python 2? – Andrew Allen May 21 '19 at 08:20
  • Yes, python 2.7.15. range() must return an iterator or some such rather than a list these days. You can wrap it in a list comprehension to expand it before concatenation though. – Simon Hibbs May 21 '19 at 08:34
0
for i in [*range(0, len(l), 10)] + [len(l)-1]:
    print("i = {}, {}".format(str(i), l[i]))

output:

i = 0, 1
i = 10, 1
i = 19, 1

or :

result = ["i = {}, {}".format(str(i), l[i]) for i in [*range(0, len(l), 10)] + [len(l)-1]] # ['i = 0, 1', 'i = 10, 1', 'i = 19, 1']
for item in result:
    print (item)

output:

i = 0, 1
i = 10, 1
i = 19, 1

I am using here * (Extended Iterable Unpacking) to pass a range as a list element to a list

* (Extended Iterable Unpacking) operator

Proposes a change to iterable unpacking syntax, allowing to specify a "catch-all" name which will be assigned a list of all items not assigned to a "regular" name.

rowsList = [*range(10)] 
print (rowsList)

output :

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

example:

print ([*range(0, 50, 10)])

output:

[0, 10, 20, 30, 40]
ncica
  • 7,015
  • 1
  • 15
  • 37
0

There's a few approaches but given you probably don't want to covert to a list if you can help it given the length of list and fact you only want to append a single element I would do the following:

Concatenate Two Ranges

https://stackoverflow.com/a/14099894/4711754

from itertools import chain
concatenated = chain(range(30), range(2000, 5002))
for i in concatenated:

So for your use case:

from itertools import chain
l=[1]*20

concatenated = chain(range(0, len(l), 10), [len(l)-1])
for i in concatenated:
    print("i = " + str(i), l[i])

# i = 0 1
# i = 10 1
# i = 19 1
Andrew Allen
  • 6,512
  • 5
  • 30
  • 73
-1

If to choose the easiest way:

n=19
l=[1]*(n+1)
for i in range(0,20,10):
    print("i = "+str(i)+","+ str(l[i]))
print("i = "+str(n)+","+ str(l[n]))
amir8778
  • 1
  • 1