sort 2 lists at the same time python
- User
sort 2 lists at the same time python
- Assistant
If you want to sort two lists at the same time in Python, where elements in one list correspond to elements in the other list, you can achieve this using the
zipfunction together withsorted.Here's an example to illustrate how to do this:
Let's say you have a list of names and a list of ages that correspond to those names:
pythonnames = ['John', 'Sarah', 'Bob', 'Alice'] ages = [25, 21, 30, 24]Now you want to sort the list of names based on the corresponding ages in ascending order. Here's how you could do it:
python