2

Let's say I have a list of tuples

users = [('Ben', 13),
         ('Ana', 13),
         ('Max', 13)]

Where ('Ben', 13) will be 0 and ('Ana', 13) will be 1 ...

EDIT

I have a table from PySimpleGUI that displays a list of tuples. Selecting a row in the table returns the row index. I want to get the 'User ID' that is tied to the returned row index so I could make use of it in a delete query.

Here's the code:

import sys
import mysql.connector
import PySimpleGUI as sg

def connect():
    mydb = mysql.connector.connect(
        host='localhost',
        user='root',
        passwd='maning',
        database='usersdb'
    )
    return mydb

mydb = connect()
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM thesisdb.users")
data = mycursor.fetchall() #the list of tuples
headings = ['User ID', 'First Name', 'Last Name', 'Username', 'Password', 'Role']


layout = [[sg.Table(values=data, headings=headings, max_col_width=25,
                        auto_size_columns=True, bind_return_key=True, justification='right', num_rows=20, alternating_row_color='lightgreen', key='users')],
          [sg.Button('Read'), sg.Button('Double')],
          [sg.T('Read = read which rows are selected')],[sg.T('Double = double the amount of data in the table')]]

window = sg.Window('Table', grab_anywhere=False, resizable=True).Layout(layout)

while True:
    event, values = window.Read()
    rowIndex = values['users'] #the index of the selected row
    sg.Popup(index) #displays the index of the selected row

As far as I know, the table is only capable of returning the index of the selected row. That's why I asked if it's possible to add an index number for each tuple because I want to use rowIndex in getting the 'User ID' with the same index number of the selected row.

The delete query will hopefully be:

mycursor.execute("DELETE FROM usersdb.users WHERE user_ID = '%s'" % (userID))

Pedro Martins de Souza
  • 1,406
  • 1
  • 13
  • 35
Maning
  • 75
  • 9
  • 6
    Python's got your back, search for [enumerate](https://docs.python.org/3/library/functions.html#enumerate) – Dani Mesejo Jan 08 '19 at 14:58
  • 1
    the `index` always exists in lists. You do not have to add anything. This is why `users[0]` returns `('Ben', 13)`. The `0` in the `[0]` is the index. – Ma0 Jan 08 '19 at 14:59
  • Something like `list(zip(users, range(len(users))))`? – applesoup Jan 08 '19 at 14:59
  • @jpp: I believe `enumerate()` is not necessarily the answer to the OP's question. `zip()` could be the desired alternative. – applesoup Jan 08 '19 at 15:02
  • @Maning, Great edit, I've reopened. – jpp Jan 08 '19 at 15:48
  • @Ev.Kounis You are right! I finally got it. I had to remove the brackets from the list and convert it to int so I could use it as a parameter in accessing the row. – Maning Jan 08 '19 at 16:34

1 Answers1

3

Comment of @Ev. Kounis was right! Each tuple in a list of tuples already has an index. The only thing left to do in order for the list to be usable as a parameter in getting the desired element of the selected row was to remove the brackets and convert it to int.

Here's the code:

raw = values['users']
x = str(raw).replace('[', '').replace(']', '')
r_index = int(x)
userID = data[r_index][0]
Maning
  • 75
  • 9