5

I have a function that I'm trying to loop through, with the purpose of amending those variables with a ", " after them.

def create_merge(address, city, state_province, country, postal_code, contact, state):
    for key in locals().keys():
        for value in locals().values():
            print(key, value)

This ...kind of works, but not really. While it gives me a proper readout of all values in that def, it only shows state for the key. I get why, because I'm looping through all values in the first key, before it moves to the second.

What I'd like to have is a loop that would produce, say:

address 123 My Address
city 
state_province Illinois
country USA
postal_code 12345
contact McGruff
state Illinois

And this, clearly just first lists the keys, then the values. How do I do one key and one value at a time?

for key in locals().keys():
    print (key)
for value in locals().values():
    print (value)

...print (key, key.values()) doesn't work.

Nor

for key in locals().keys():
    print (key, locals().values())

I think I'm getting close, but just can't get it! Thanks for any suggestions/advice.

Final note: I need to do this in a way where I can see if the value is NOT empty, and if not empty, amend the value with ", " before the function continues.

BruceWayne
  • 22,923
  • 15
  • 65
  • 110
  • Could you provide the variable you are trying to iterate through? Do you have a dictionary like this, or are you looking to pass the variables into the function through another loop? location = {'address' : '123 My Address', 'city' : None, 'state_province' 'Illinois', 'country' : 'USA', 'postal_code' : 12345, 'contact' : 'McGruff', 'state' : 'Illinois'} – Dom DaFonte Jun 22 '17 at 21:33

2 Answers2

2

You can loop through the keys and print out the key and the value of the key using the .get method.

for key in locals().keys():
    print(key, locals().get(key))

Alternatively, you could just use indexing to get the value.

for key in locals().keys():
    print(key, locals()[key])

Finally, you could also use .items() of dictionaries. (.iteritems() for Python 2.x)

for key, value in locals().items():
    print(key, value)

Make sure you instantiate key and value before you use these variables. Otherwise, you will change the items inside locals() while iterating and you will get an error.

victor
  • 1,573
  • 11
  • 23
  • I get "RuntimeError: dictionary changed size during iteration` when doing those. Edit: Aha! Your edit did the trick, looping through `items()`. :D – BruceWayne Jun 22 '17 at 21:37
0

While @VictorC's for key, value in locals().items() worked, I found that this does too:

for key, value in zip(locals().keys(), locals().values()):
    print (key, value)

(Just noting this here for the record. I'm still learning python, so I'm very open to someone commenting on the difference in zip() vs. looping through items() if they are bored).

BruceWayne
  • 22,923
  • 15
  • 65
  • 110
  • 1
    I'm not sure about what the difference in performance is, but I do know that `locals().items()` returns the same result as `zip(locals().keys(), locals().values())`. My assumption is that `locals().items()` only requires iteration through `locals()` once, while the `zip` method will require two iterations, once to get the keys and once to get the values. – victor Jun 22 '17 at 21:49