0

I am new to python and I want to know how to write for loop for the function below.

def row_column_number(data):
    
    rows, columns = data.shape
    
    print(f'Number of rows for the dataset are {rows}')
    print(f'Number of columns for the dataset are {columns}')

I tried this but this is not what I like

>> for dimensions in enumerate(data.shape):
>>     print(f'Number of {dimensions}')

Number of (0, 4803)
Number of (1, 24)

How can I write a for loop for the {rows, columns} variable above.

Number of {for-loop variable} for the dataset are {for-loop value}

Armando Bridena
  • 237
  • 3
  • 10
  • 1
    The for loop uses a specific variable; you can't change its name each time through the loop. Also, you can't access the variable's name from the value; you can only access it from the variable, in which case you already know what the name is. What you want to do is use **strings** to store names for the dimensions. Variable names aren't storage. Variable names are there so that you can say which variable you mean when you write the code. – Karl Knechtel Aug 12 '22 at 02:48
  • You said, "I want to know how to write for loop for the function below.". Can you please elaborate? – Shrirang Mahajan Aug 12 '22 at 02:53
  • Instead, make a separate **list of strings** for the names that you want to use, and then iterate in parallel - see the linked duplicate. I was going to try to write up an answer to explain the conceptual idea behind why we want to do it this way - but it's really straightforward once you have the idea, and it's hard to make a good Q&A out of it. The problem is, if anyone else actually is confused in the same way, it doesn't result in a question that makes sense to type into a search engine, and it definitely wouldn't find this post. – Karl Knechtel Aug 12 '22 at 02:59

0 Answers0