Remove bracket of List

Hi, please help me how to remove the brackets in the List result.

This the code I have:

    total = [r['total'] for r in app_tables.accounts.search()]
    self.label_total.text = '$' + str(total)

The output in the Form is this:
$[5000]

The desired output is (without the brackets) : $5000

Knowing what’s inside a table row would help.

What is the output of:

print(dict(app_tables.accounts.search()[0]))

This is the output:

{'capital': 5000, 'total': 5836, 'profit': 836}

Thanks for reply.

app_tables.accounts.get(<conditions>) returns the row that satisfies the conditions.
app_tables.accounts.search(<conditions>) returns a list of rows that satisfy the conditions (it’s actually an iterator, which often behaves just like a list).

If your table has only one row, then you should use get.

If you use search, you will get a list of rows, and a list of rows converted to string has the square brackets.

It looks like your table has only one row, and the column total on that row is 5000 (well, according to your first post, while according to your second one it’s 5836, but that’s another story).

row = app_tables.accounts.get()
self.label_total.text = f"${r['total']}"
2 Likes

Thanks a lot stefano.menci.

Really appreciated the explanation on usage & result differences of search and get.

Indeed I misread the column value of total. The correct value is $5836.

1 Like

Regardless of table size, if your search result is expected to have at most one row, then get is usually the best choice:

  • zero results: returns None
  • one result: returns that row as a dict-like object
  • more than one result: raises an exception.

Otherwise, search is usually your best choice.