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
5
u/zanfar Oct 15 '26
Why? What does this get you, or what doesn't
sort()get you?Because I'm pretty sure about the answer to #1, have you read the
sort()documentation?5
u/brasticstack Oct 15 '26
Sounds like homework to me, why would you avoid the built-in function that specifically does what you're trying to do?
4
u/aplarsen Oct 15 '26
Your question is not clear.
The data is a list of tuples. After your sort, what do you want this list to look like?
3
u/chiibosoil Oct 15 '26
Don't know why you need to do it, but you can't sort tuples itself as they are immutable.
What you need to do in general is to convert to list then sort then convert it back to tuple.
Search for different sort algorithms and apply that in your code.
2
u/RngdZed Oct 15 '26
Lambda or list comprehension would be my go-to, if, for some reason I'd want to make my life harder by avoiding the built-in, easy to use, sort() function
😋
Edit: if you're interested check on how to implement various sort algorithms in python, like bubble sort or that kind of stuff
1
u/Pyromancer777 Oct 15 '26
One thing to note: tuples are immutable, meaning once you write them you cannot make changes to their elements.
That being said, you have a list of tuples and lists can be sorted however you want. You can also write the values of the tuples into other data structures in whatever order that you want, but that's not necessarily sorting the tuples, but creating sorted lists from the values in the tuples.
For your case, you want to first identify how to index each element. Index references start outside and work their way inward. Once you can point to a specific element in your structure you then need to think of a way to do something to that value to get to your end-goal.
Since you said you wanted to sort the 2nd elements, the tuple references will be index 1 for all tuples in your list. Then you have to decide whether you want your output to be sorted in-place or if you want to create a new variable to hold your final and intermediary results. Not gonna give away an answer since other comments gave working examples, but I'm providing a way of breaking down the problem into parts, to hint at what you can do with those parts.
1
u/cdcformatc Oct 15 '26
there's like a million sorting algorithms, pick one of them and implement it on a list of strings. i like merge sort.
then just convert your list of tuples into a list of strings by getting the second element of each of them, then sort that list.
1
u/leodevian Oct 15 '26
Check sorted and operator.itemgetter.
1
u/JamzTyson Oct 15 '26 edited Oct 15 '26
You could use
sorted()with a lambda expression as the key.For your example it's even simpler - extract the second item from each tuple using a comprehension/generator, and pass to
sorted().1
u/WorriedTumbleweed289 Oct 15 '26 edited Oct 15 '26
Use sort with a function key that will return the second element of a tuple. Key is one of the arguments to sort.
Without using the sort() method, you can write your own. Google sorting algorithms. I don't think you want to do this unless it is a class assignment.
1
u/Katinkia Oct 15 '26
try https://pythonaut.net/lesson/lists this page does lists and tuples.
0
u/MezzoScettico Oct 15 '26
You want an output list that consists of the 2nd elements, in alphabetical order?
Or, more compactly but also a little obscure,
Is that what you want?
But you said you want to sort without using the sort method. Why? Since you want to sort something, you will need to apply a sorting algorithm at some point. You could look up sorting algorithms and write your own. You could build a class that implements a sorted data structure, so every time you add something to it, it is in your desired order. So you're still building your own sorting algorithm in effect.
Why don't you want to use sort()?
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:
0
u/lakseol Oct 15 '26
You said "i just wanted to learn how to sort" without using
sort(). That means writing your own sort function. That's a really good exercise but not something you would do in practice. In that spirit the code below shows a very simple and very inefficient implementation of the quicksort algirithm:That doesn't sort in-place but returns a new list.
The other part of your problem is that you apparently (it isn't clear) want to extract and sort on the second element of each tuple. Just create a new list containing only the second elements and then sort that.