1

I am trying to figure out how to use the code below with list comprehension.

link = 'page={}&index={}'
index = 10
links = []
for page in range(2, 4):
    links.append(link.format(page, index))
    index += 10

I have tried many different ways and Googled as much as possible (maybe I am not searching for the correct terms?). I am still unable to figure it out. Below is one of the ways I tried but I get a SyntaxError error.

link = 'page={}&index={}'
index = 10
links = [link.format(link, index) for page in range(2, 4) index += 10]

This should be the output of the list comprehension:

['page=2&index=10', 'page=3&index=20']

If anyone has any ideas it would be greatly appreciated it. Thank you!

antfuentes87
  • 849
  • 3
  • 17
  • 34
  • Possible duplicate of [How can I do assignments in a list comprehension?](https://stackoverflow.com/questions/10291997/how-can-i-do-assignments-in-a-list-comprehension) – dfundako Jun 15 '18 at 14:35
  • 4
    Broadly speaking, A list comprehension can contain only expressions. `index += 10` is not an expression; it is a statement. So you can't put it inside a list comprehension. But even if you could do this, you should question whether you should. Not all code is improved by making it shorter. – Kevin Jun 15 '18 at 14:36
  • @dfundako I edited the question to show my expected output. Thanks. – antfuentes87 Jun 15 '18 at 14:36
  • @Kevin Oh ok, well that is fine too, not the end of the world if I can't do this. I just could not find anywhere that talked about doing this, etc... Thank you for that information. – antfuentes87 Jun 15 '18 at 14:37
  • @Kevin Great thing to remember when learning to code. Code will be written once, but read a hundred times. Make it easier to read and comprehend instead of saving a line or two of code. – dfundako Jun 15 '18 at 14:37

2 Answers2

4

You can use the enumerate: builtin function to increment the index:

>>> [link.format(page, i*10) for i, page in enumerate(range(2, 4), start=1)]
['page=2&index=10', 'page=3&index=20']

This will also work with any other iterable instead of just a range(2, 4), e.g. a list of strings. Any such iterable can thus be augmented with a counter variable, like index in your example.

tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • 1
    `enumerate` is overkill here, given that `i == page - 1` – chepner Jun 15 '18 at 14:41
  • 1
    @chepner Well, yes, but it is more generally applicable. Instead of `range(2, 4)`, you could have a range with steps, or just any iterable. – tobias_k Jun 15 '18 at 14:42
3

You can't use += statements (or any statement for that matter) in a list comprehension. In your case, use zip and itertools.count:

import itertools
[link.format(page, index) for page, index in zip(range(2, 4), itertools.count(10, 10))]
FHTMitchell
  • 11,793
  • 2
  • 35
  • 47
  • yes what chepner said (almost) `[link.format(page, 10*(page-1)) for page in range(2, 4)]` – Chris_Rands Jun 15 '18 at 14:40
  • 2
    (I deleted my comment when I noticed I didn't have the relationship between page and link quite right; Chris got it, though.) – chepner Jun 15 '18 at 14:42
  • I am not sure if you want to make your comment into a answer so I can mark it right @Chris_Rands but this is exactly what I was trying to figure out. Thank you for making such a simple and straight to the point answer. – antfuentes87 Jun 15 '18 at 14:46
  • @antfuentes87 I mean, they all do exactly the same thing... but it's your call I guess. I agree with @tobias_k though, `enumerate`/`itertools.count` are more versatile. – FHTMitchell Jun 15 '18 at 14:46
  • 1
    I do agree but I have to import itertools for yours to work and I do not have to with @Chris_Rands. Plus I had a very similar attempt when I was trying to figure it out myself. – antfuentes87 Jun 15 '18 at 14:49
  • @antfuentes87 I have to disagree with that. Don't ever be afraid of importing standard library modules, especially `itertools`, `collections` and `functools`. It will save you a lot of repetition. `itertools` is even a zero-cost import since it's used by python on startup. What about tobias_k's answer then? `enumerate` is a built in function. – FHTMitchell Jun 15 '18 at 14:54
  • If you are allergic to imports, you can do `[link.format(page, index) for page,index in zip(range(2,4),range(10,30,10))]` The key to this answer is `zip` with two increasing iterators... – dawg Jun 15 '18 at 15:01