Python Forum
Loop through array items dynamically in a certain format
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop through array items dynamically in a certain format
#1
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?
Reply
#2
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
I've restored the post content.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  loop through csv format from weburl in python maddyad82 3 358 Jan-17-2024, 10:08 PM
Last Post: deanhystad
  Loop over an an array of array Chendipeter 1 526 Nov-28-2023, 06:37 PM
Last Post: deanhystad
  Dynamically allocated array JESuh 1 918 Jun-29-2022, 10:53 AM
Last Post: Larz60+
  Populating an array dynamically zxcv101 1 1,106 May-17-2022, 11:24 AM
Last Post: deanhystad
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 5,763 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  Loop different actions for an array Tibovdv 4 2,703 Mar-25-2021, 06:46 PM
Last Post: jefsummers
  Comparing Items Different Data Frames With a WHILE Loop JoeDainton123 1 1,926 Jul-30-2020, 04:11 AM
Last Post: scidam
  Double for loop with dates in array leifeng 1 1,597 Apr-05-2020, 03:27 PM
Last Post: leifeng
  Can't seem to figure out how to put all of the lists items from a loop into 1 list Cosmosso 4 2,670 Feb-21-2020, 02:40 PM
Last Post: Cosmosso
  removing items from a list or group within a for loop. allusernametaken 9 4,464 Nov-13-2019, 01:58 AM
Last Post: allusernametaken

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020