pythonfor-loop

Different variables in same "for" loop?


I have some data (ID = Name), and I want to put the names of the given people IDs in a QTableWidget, but I'm having problems with a for loop...

Here's my code:

for x in people:
  for y in range(len(people)):
   view.setItem(y, 0, QtGui.QTableWidgetItem(name[x][:-1][1:]))

where y in view.setItem(y, 0, QtGui.QTableWidgetItem(name[x][:-1][1:])) is the row index on table, I want to make a table with the names of the given IDs.

This is the result with 3 IDs given:

|  Jhon   |
|  Jhon   |
|  Jhon   |

and this is what I'd like it to display:

|  Sally  |
|  kate   |
|  Jhon   |

any help?


Solution

  • I think you want enumerate here.

    for idx,x in enumerate(people):
        view.setItem(idx, 0, QtGui.QTableWidgetItem(name[x][:-1][1:]))