Return a random row from a data column

I am trying to return a random row of a column (questions_column) into a label(self.label_2)
This is what I’m trying

    my_list = app_tables.questions_choices.search()
    for row in my_list:
      item =random.choice(row['question_column'])
      self.label_2.text = item

The return is a random letter in the last row of the column,rather than a single random row from the column.
I have also tried

    my_list = app_tables.questions_choices.search()
    rand_idx = randint(0,len(my_list)-1)
    self.label_2 = my_list[rand_idx]

but then the return is
<LiveObject: anvil.tables.Row>
If someone could explain this return to me it would be greatly appreciated as it’s not the first time I have seen it.

I’ll translate your code from Python to English, so you understand what your current code is doing:

search the questions_choices table and put the list of rows in the variable my_list
for each row in my_list:
  pick a random item from the question_column value
  and put it in label_2

So you are getting all the rows, then iterating through them and for each row you are getting its question_column value and picking a random item from it. Since the question_column value is a string, a random item of that string is a letter. The cycle sets label_2 many times, once per row. It’s fast, so you only see a random letter of the question column of the last row in the table.

I don’t know what you are trying to do, but assuming that you want to pick 5 random rows and then get their question_column value, this should work:

    my_list = list(app_tables.questions_choices.search())
    for n in range(5):
      row = random.choice(my_list)
      item = row['question_column']
      print(n, item)

After reading a few of your questions, I think you have some confusion about what a column is. You seem to be referring to a column as an object containing all the values of a column of the table.

There is no such thing.
When you define a table, you define the columns, but when you use it (add or search or read or delete stuff from it), you deal with rows. One row at a time. Python sees columns as a attributes of one row at a time.

I would rephrase your question from return a random row of a column into return a specific column of a random row of a table.

PS: I did wrap the table search in a list(...), but I would say that belongs to another topic. You can ask another question to address the difference between the iterator returned by the search and the list created by wrapping the iterator in a list(...)

Thank you Stefano !
That helps a lot
I appreciate your thorough answer

I have tried the code Stefano suggested

    for n in range (5):
      row = random.choice(my_list)               #this is row 24
      item = row['question_column']
      self.label_2.text = (n,item)

but get the following error

SuspensionError: Cannot call a function that blocks or suspends here at [Homeform.questionForm, line 24](javascript:void(0)) called from [Homeform.navigation, line 32](javascript:void(0)) called from [Homeform, line 44](javascript:void(0))

Have you wrapped the search in a list?
image

Can you share a clone link (considering that you will be cloning also your tables)?

2 Likes

Grrrrr !
Sooo much to learn
Thanks

1 Like