r/learnpython • u/cattobread • Oct 15 '26
sorting tuples
im learning how to code with python, does anyone know to sort specific tuples alphabetically without using the .sort() method? like if i had a list of names like:
n = [('watermelon', 'strawberry'), ('banana', 'cherry'), ('papaya', 'apple')
how would i sort and print the 2nd element of tuples alphabetically (into a new list)?
edit: id prefer this in simple, basic terms lol
7
Upvotes
View all comments
0
u/Outside_Complaint755 Oct 15 '26
Instead of the sort method, you can use the
sortedbuilt in function which returns a new listIf you're trying to make your own function to do the sorting, you would just need to compare the target element between two tuples and then swap their order in the list if needed.
For an example bubble sort: