python – Remove brackets from sorted() function

Question:

He was creating a quadratic function calculator and wanted to arrange the roots in ascending order. Searching, I arrived at the sorted() function.

raiz1 = round((-b + math.sqrt(delta)) / (2 * a), 2)
raiz2 = round((-b - math.sqrt(delta)) / (2 * a), 2)

raizes = [raiz1, raiz2]

if delta > 0:
    print(f"A função tem duas raízes reais, que são: {sorted(raizes, key=int)}.")

The result of this is that I'm going to have the roots in square brackets instead of parentheses, as is the default. I know this is not a big issue, but I think in the future there will be situations where I don't want the numbers in square brackets, so I would like to know if there is any way to suppress them.

Answer:

sorted returns a list , and when you print a list, the elements are shown in square brackets, so it's not that " sorted returns the square brackets ", it's that the square brackets are part of the textual representation of a list, that is, the way it was chosen to display a list in text form (but the square brackets themselves are not part of the list as it only has values).

If you don't want it to show the square brackets, then don't print the list directly. An alternative is to transform the list into a single string, using join , and then you put parentheses around it:

print(f"A função tem duas raízes reais, que são: ({', '.join(map(str, sorted(raizes, key=int)))}).")

Note the parentheses before and after the { and } : they will be around the string generated by join . In this case, join will put the values ​​separated by , (comma and space – and you can change it to whatever you want).

Or, to make it a little clearer what's going on:

raizes_texto = ', '.join(map(str, sorted(raizes, key=int)))
print(f"A função tem duas raízes reais, que são: ({raizes_texto}).")

In this case, I also used map together withstr to transform the values ​​into strings, because the join gives an error if the elements are not strings.


I don't think it makes much sense to use key=int , as then you're sorting the roots by their integer values. So if you have:

raizes = [2.5, 2.4]

When transforming them to int , both will be equal to 2 , so they won't change order, as the documentation says that the ordering is stable, that is, it doesn't change the order of 2 elements if they are considered equal. So, as you did, the roots won't be sorted if they have decimal values. If you want to sort them, just use sorted and that's it:

raizes_texto = ', '.join(map(str, sorted(raizes)))
print(f"A função tem duas raízes reais, que são: ({raizes_texto}).")

Another alternative is to transform the list into a tuple , because tuples, when printed, show the elements in parentheses:

print(f"A função tem duas raízes reais, que são: {tuple(sorted(raizes))}.")

Although this one I find a bit "humble" (as well as solutions that replace the brackets, or anything else that depends on the specific implementation of how the list is printed). A replace may seem "innocent" and even work for this particular case, but you just have a list where one of the elements is a string whose text contains a square bracket, for everything to stop working (or a list of lists, for lists internals are also printed with square brackets and these will be removed improperly, see example ).

Anyway, if you want a specific format, then construct the string explicitly, according to what you want.

Scroll to Top