Dear Python experts, could you please shed a light on how to iterate over a list of tuples properly? (Edit: And how to make my code work.)

I’m gonna try to explain this issue in a succinct way, but feel free to ask for more details if you want it.

I’m using the following code to get the card ids stored in a table in a sqlite3 file:

info = cursor.execute(
        '''SELECT cid FROM events''' ).fetchall()

This code returns the following list of tuples:

[(None,), (None,), (None,), (None,), (None,), (None,), ('1631565234935',), ('1582147898059',)]

In order to get each one of the values in this list I use:

info = [(None,), (None,), (None,), (None,), (None,), (None,), ('1631565234935',), ('1582147898059',)]
for x in info:
    for y in x:
        print(y)

Which returns:

>>> info = [(None,), (None,), (None,), (None,), (None,), (None,), ('1631565234935',), ('1582147898059',)]
... for x in info:
...     for y in x:
...         print(y)
None
None
None
None
None
None
1631565234935
1582147898059

However, if I try to run the same code inside a function, it returns no output:

def getCid(self):
    info = [(None,), (None,), (None,), (None,), (None,), (None,), ('1631565234935',), ('1582147898059',)]
    for x in info:
        for y in x:
            print(y)

like this:

>>> def getCid(self):
...     info = [(None,), (None,), (None,), (None,), (None,), (None,), ('1631565234935',), ('1582147898059',)]
...     for x in info:
...         for y in x:
...             print(y)
<no output>

I’ve also tried:

info = [(None,), (None,), (None,), (None,), (None,), (None,), ('1631565234935',), ('1582147898059',)]
for x in info:
    for y in x:
        print(y)

def getCid(self):
    print(y)

Which works fine in the Debug Console:

However, if I use the following code and try to use the value of “y” to be executed inside the function, the add-on always returns the last value of the list.

info = cursor.execute(
        '''SELECT cid FROM events''' ).fetchall()
for x in info:
    for y in x:
        cid = y

def notWorkingFunction():
(...)
    if cmd == "display-cid":
        (...).setText("cid:" + str(cid))

What I’m trying to achieve, instead, is: if I click one item, it will return the correspondent cid, if I click another item, it will return this item’s cid, and so on.

It’s also intriguing that I can perfectly get the values of the card’s ids inside another function in the same module using:

def anotherFunction(content):

data = cursor.execute(
        '''SELECT * FROM events ORDER by date(aGivenDate) %s''' % "ASC").fetchall()
for row in data:
variable = row[2]
(...)
cid = row[14]
(...)
content.stats +=  "<b title="+ str(cid) +">" + variable + "</b>"
(...)

What the function above does is that when I hover the mouse over the text it displays a tooltip showing the cid of the card related with that particular text, however, if I click on it, it only returns the last cid of the list (as I stated before), even if the text is not related to it.

Could you please point what’s wrong with my approach here?

It looks to me like you are defining the function but not actually calling it. You need to call the function after you define it, e.g., getCid(). However, as you’ve written the function, getCid() will not work. You should redefine it as a function which takes no arguments, i.e. you should remove self when defining the function, since you are not actually using the argument in the function.
This should work:

>>> def getCid():
...     info = [(None,), (None,), (None,), (None,), (None,), (None,), ('1631565234935',), ('1582147898059',)]
...     for x in info:
...         for y in x:
...             print(y)
... getCid()
2 Likes

Hi @andrewsanchez. Thank you for your fast reply.
This suggestion of yours did work. It returned each one of the elements of the list in the console.
I’m still not able to make each link return the corresponding cid, though.

I would highly recommend that you write up an analogous example and run it on https://pythontutor.com/ to understand why your code doesn’t work the way you expect. You can also insert print statements to see show the value of the variable of interest in each iteration of the for loop.

2 Likes