Python Forum

Full Version: Loop through array items dynamically in a certain format
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have something like below code:

arr = ['foo','bar']
def func():
  return f"""
  MY_STATEMENT (
    {{
      {arr[0]}: ${arr[0]},
      {arr[1]}: ${arr[1]},
      other_fixed_keys: 'some value'
    }}
  )
  """
But the length of arr is unknown. It may be 0, 1, 2, ... n. So, how can I iterate it so that the above statement will be okay?
I observe that this not array, this is list. It's important in Python, because numpy has arrays and they are totally different beasts. You know - 'In the face of ambiguity, refuse the temptation to guess' and all Smile
(Jun-05-2019, 11:19 AM)bhojendra Wrote: [ -> ]How can I delete this post?

We're not big on deleting posts around here, and please don't delete post content.
I've restored the post content.