-3

I want to call a python for loop with two iterables like this:

TEMPLATE_FILE = { 'a': 'power', 'b': 'voltage', 'c': 'current' }
for (script in TEMPLATE_FILE.values()) and (files in TEMPLATE_FILE.keys()):
    print 'script: ',script
    print 'files: ',files 
    print "\n"

But that is a syntax error, how can I do it?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
nk14
  • 29
  • 5

4 Answers4

10
 for files,script in TEMPLATE_FILE.items():
    print(files,scripts)

is the construction you're looking for.

(in python 2 there's an iteritems which is removed in python 3 so for small dictionaries items is OK and portable)

of course you can do:

for files in TEMPLATE_FILE:
    scripts = TEMPLATE_FILE[files]

but that's not as efficient as you're hashing the key at each iteration, whereas you could get the values without that. Reserve hashing for random access cases.

Note that you can iterate through sorted keys like this (frequent question):

 for files,script in sorted(TEMPLATE_FILE.items()):
    print(files,scripts)
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • 2
    I haven't downvoted (I swear!) but the first answer was mine (by 16 seconds) ;) – S. de Melo Oct 20 '16 at 07:23
  • 1
    fair enough, but your answer only proposed to iterate on the keys and not keys+values. Your answer was not "equivalent" at the time. You edited it afterwards (I would have deleted mine if that was the case). Check the downvoted answer below that proposes only the first version... – Jean-François Fabre Oct 20 '16 at 07:41
  • True, I wanted to check the Python version (hoping it was 3) before adding the other part. Not important anyway. – S. de Melo Oct 20 '16 at 07:46
  • `items` also works in python 2. I tend to answer directly with python 3 (2 compatible) constructs even if they're not as optimal in python 2. same with `range` and `xrange`. – Jean-François Fabre Oct 20 '16 at 07:51
  • I know but `iteritems` is better in Python 2, even if I avoid it now so that the code works in both versions. – S. de Melo Oct 20 '16 at 07:52
  • of course it is. I should just have found the duplicate instead of answering, because the answer is really better than all the answers here. The only advantage is that now we can clearly privilege python 3 (the answer was marked as a duplicate of a totally unrelated question, that's why I answered in the first place) – Jean-François Fabre Oct 20 '16 at 07:54
3

To access key, value in dictionary you can do like this.

For Python 2.x:

for files,script in TEMPLATE_FILE.iteritems():

For Python 3.x:

for files,script in TEMPLATE_FILE.items():

Or you can also do this, independent of python version:

for files in TEMPLATE_FILE:
    script = TEMPLATE_FILE[files]
    #print(files, script)

The last bit has the drawback of re-hashing the keys each time as suggested by @Jean-FrançoisFabre.

sinsuren
  • 1,745
  • 2
  • 23
  • 26
  • `for files,script in TEMPLATE_FILE.items()` works with python 2. It's just that you don't iterate but get all the items at once. Not a problem for small lists. The last bit has the drawback of re-hashing the keys each time, whereas you already have the values handy. – Jean-François Fabre Oct 20 '16 at 07:43
2

You just want to iterate over a dictionary and get the key and its value at each iteration.

for file_ in TEMPLATE_FILE:
    script = TEMPLATE_FILE[file_]

or

for file_, script in TEMPLATE_FILE.items():
    ...
S. de Melo
  • 786
  • 4
  • 11
0

you don't need to code like this. the following code is what you want

TEMPLATE_FILE = { 'a': 'power', 'b': 'voltage', 'c': 'current' }
for script in TEMPLATE_FILE:
    print 'script: ',script
    print 'files: ',TEMPLATE_FILE[script ]
    print "\n"
Mehrdad Dadvand
  • 340
  • 4
  • 19