1

I am trying to run my script from Jenkins which has Python 2.6 installed. My script was originally written on a Linux machine which uses 2.7.5. Whenever I run the script form my local machine it works fine, but when I try to run it from Jenkins, it throws a syntax error for the following code:

rpmDict = {rpmList[i]: rpmList_full[i] for i in range (len(rpmList))}

rpmDataDict = {rpmDataTextList[i]: rpmDataTextList_full[i] for i in range (len(rpmDataTextList))}

Can someone help me translate this to 2.6 syntax?

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271

2 Answers2

3

So, in both versions this is totally over-engineered.

rpmDict = {rpmList[i]: rpmList_full[i] for i in range (len(rpmList))}

Should just be:

rpmDict = dict(zip(rpmList, rpmList_full))

And:

rpmDataDict = {rpmDataTextList[i]: rpmDataTextList_full[i] for i in range (len(rpmDataTextList))}

Should just be:

rpmDataDict = dict(zip(rpmDataTextList, rpmDataTextList_full))

But as the other answer has noted, in Python2.6,

{expression0: expression1 for whatever in some_iterable}

Can be converted into

dict((expression0, expression1) for whatever in some_iterable)

Note also, you really should be using Python 3 as much as possible. In any case, in Python 2, use:

from future_builtins import zip

So that zip creates an iterator, not a list, which is more efficient, especially for larger data.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • 1
    Was about to post the same basic answer (just with an intermediate step of `zip`+genexpr), up-voted yours instead. Just wanted to note that `from future_builtins import zip` avoids making a pointless temporary `list` from `zip` on Python 2. – ShadowRanger Sep 23 '20 at 14:59
2

Just use the dict constructor and pass it a generator expression of tuples:

rpmDict = dict((rpmList[i], rpmList_full[i]) for i in range(len(rpmlist)))
tzaman
  • 46,925
  • 11
  • 90
  • 115