Create loop and move to arguments of a function?

Asked

Viewed 37 times

1

I have the following code in Python:

storage.tableWidget.setColumnWidth(0, 30)
storage.tableWidget.setColumnWidth(1, 140)
storage.tableWidget.setColumnWidth(2, 90)
storage.tableWidget.setColumnWidth(3, 55)
storage.tableWidget.setColumnWidth(4, 60)
storage.tableWidget.setColumnWidth(5, 40)

it accesses each column in a tableWidget(first argument) and sets the cell size (second argument).

I lack knowledge, but I believe it is possible to decrease this code with some loop, keeping in mind that the first argument is sequential (0,1,2,3,4,5). Someone can give me a light?

thank you.

  • 1

    https://ideone.com/dIV9D7

1 answer

2


You can do using enumerate:

tamanho_celula = [30, 140, 90, 55, 60, 40]

for col, tamanho in enumerate(tamanho_celula):
    storage.tableWidget.setColumnWidth(col, tamanho)

In the first line you create a list of cell size values and inside the for you iterate.

  • The enumerate searches the index together?

  • 1

    it adds an "counter" to the for loop, the enumerate accepts that you put as an optional argument of which number it will start counting.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.