0

Suppose we have a function

def analyse(n):
      for c in cluster_{n}:
             do something

For example if we run analyse(1) I want it to run like

for c in cluster_1 :
         do something

where cluster_1 is a global list already declared.

How can I do that?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

1

As mentioned in comments, you can pass the list as argument directly.

Another way to do so is to create a dictionary and store all the global lists in it, like this;

obj = {"cluster_1": [1, 2, 3, 4, 5]}

def analyse(n):
      for c in obj[f'cluster_{n}']:
             print(c)


analyse(1)

OUTPUT:

1
2
3
4
5
Irfan wani
  • 4,084
  • 2
  • 19
  • 34