3

Is there a way to simplify this try/except into a one line with lambda?

alist = ['foo','bar','duh']

for j,i in enumerate(alist):
  try:
    iplus1 = i+alist[j+1]
  except IndexError:
    iplus1 = ""

Is there other way other than:

j = '' if IndexError else trg[pos] 
alvas
  • 115,346
  • 109
  • 446
  • 738
  • 2
    So... what are you trying to accomplish, here? – Ry- Jan 08 '13 at 05:48
  • It's suppose to do some dynamic programming where i see previous and next item in list and make some decisions base on some convergence that i'll calculate. – alvas Jan 08 '13 at 05:53
  • I assume there's more code below the `try`/`except`, right? Otherwise there's no point in the loop, since `iplus1` will just get the last value at the end. One question though: does the omitted code write new values into the list as you're iterating over it? That can work in some situations, but it is easy to do incorrectly if you're not careful. – Blckknght Jan 08 '13 at 06:21
  • nope, if iplus1 dont exist, it remains as a null string. – alvas Jan 08 '13 at 06:22
  • I'm not sure I understand what you're doing then. It seems to me that your loop is equivalent to `if alist: iplus1 = ""`. None of the `i+alist[j+1]` values will ever be seen, since they overwrite each other, and then get overwritten by the empty string when the exception happens (and it will always happen if `alist` is not empty). – Blckknght Jan 08 '13 at 06:29
  • @blckknght i'm trying to do something like this http://stackoverflow.com/questions/14209506/how-to-access-2-list-at-a-time-while-looking-ahead-with-izip-longest-python – alvas Jan 08 '13 at 06:39
  • OK. My point was that you need to *do something* with `iplus1` inside the loop after you calculate it, otherwise there's no point in having the loop at all. In the followup question you linked, you're printing it (along with some other values). That's the "more code after the `try`/`except` I was asking about. – Blckknght Jan 08 '13 at 09:13

1 Answers1

6

No, Python doesn't have any shorthands or simplifications to the try/except syntax.

To solve your specific problem, I would probably use something like:

for j, i in enumerate(alist[:-1]):
   iplus1 = i + alist[j + 1]

Which would avoid the need for an exception.

Or to get super cool and generic:

from itertools import islice

for j, i in enumerate(islice(alist, -1)):
    iplus1 = i + alist[j + 1]

Alternative, you could use: itertools.iziplongest to do something similar:

for i, x in itertools.izip_longest(alist, alist[1:], fillvalue=None):
    iplus1 = i + x if x is not None else ""

Finally, one small note on nomenclature: i is traditionally used to mean "index", so using for i, j in enumerate(…) would be more "normal".

kojiro
  • 74,557
  • 19
  • 143
  • 201
David Wolever
  • 148,955
  • 89
  • 346
  • 502
  • 1
    You'll also need to add the last element without a suffix. – Sudhir Jonathan Jan 08 '13 at 05:52
  • Yes, that would definitely need to be considered. – David Wolever Jan 08 '13 at 05:56
  • _i is traditionally used to mean "index"_ - still, it's a good habit to give more discernible names even to indices, so that they don't get lost in a loop longer than 2 lines. One-letter variable names are a terrible heritage from the days of perforated cards, and should be abandoned! – volcano Jan 08 '13 at 06:12