Python Forum
Loop through array items dynamically in a certain format - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Loop through array items dynamically in a certain format (/thread-18888.html)



Loop through array items dynamically in a certain format - bhojendra - Jun-05-2019

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?


RE: Loop through array items dynamically in a certain format - perfringo - Jun-05-2019

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


RE: Loop through array items dynamically in a certain format - ichabod801 - Jun-05-2019

(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.


RE: Loop through array items dynamically in a certain format - micseydel - Jun-11-2019

I've restored the post content.