Python: Sort list of lists numerically

0 votes
I have a list of x,y coordinates and I want to sort them based on the x coordinate, then y coordinate when x is the same and eliminate duplicates of the same coordinates. For example, if the list is:

[[450.0, 486.6], [500.0, 400.0], [450.0, 313.3], [350.0, 313.3], [300.0, 400.0],
 [349.9, 486.6], [450.0, 313.3]]
 

I want to rearrange it to:

[[300.0, 400.0], [349.9, 486.6], [350.0, 313.3], [450.0, 313.3], [450.0, 486.6],
 [500.0, 400.0]]

Can anyone tell me the solution?
May 20 in Python by ana1504.k
• 7,890 points
214 views

1 answer to this question.

0 votes
You can try and De-dupe it with a dict.

>>> L = [[450.0, 486.6], [500.0, 400.0], [450.0, 313.3], [350.0, 313.3], [300.0, 400.0], [349.9, 486.6], [450.0, 313.3]]
>>> sorted({tuple(x): x for x in L}.values())
[[300.0, 400.0],
 [349.9, 486.6],
 [350.0, 313.3],
 [450.0, 313.3],
 [450.0, 486.6],
 [500.0, 400.0]]
answered May 20 by SDeb
• 13,210 points

Related Questions In Python

0 votes
1 answer

Python: list of lists

Lists are a mutable type - in ...READ MORE

answered Aug 2, 2018 in Python by bug_seeker
• 15,360 points
52 views
+1 vote
4 answers

Count the frequency of an item in a python list

To count the number of appearances: from collections ...READ MORE

answered Oct 18, 2018 in Python by tinitales
2,175 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
868 views
0 votes
1 answer

Where can I get the list of Python keywords?

Just import a module “keyword”. Here you ...READ MORE

answered Apr 19, 2018 in Python by aayushi
• 750 points
26 views
0 votes
1 answer

Crawling after login in Python

You missed a few login data forms, ...READ MORE

answered Sep 7, 2018 in Python by Priyaj
• 57,300 points
451 views
0 votes
1 answer

Crawling after login in Python

You missed a few login data forms, ...READ MORE

answered Sep 14, 2018 in Python by Priyaj
• 57,300 points
46 views
0 votes
1 answer

“stub” __objclass__ in a Python class how to implement it?

You want to avoid interfering with this ...READ MORE

answered Sep 27, 2018 in Python by Priyaj
• 57,300 points
78 views
+1 vote
1 answer

How is raw_input() and input() in python3.x?

raw_input() was renamed to input() so now input() returns the exact string ...READ MORE

answered Oct 30, 2018 in Python by Priyaj
• 57,300 points
65 views
0 votes
1 answer

Sort a part of a list in place

You can write it as such: a[i:j] = ...READ MORE

answered Feb 12 in Python by SDeb
• 13,210 points
34 views
0 votes
1 answer

Transpose list of lists

You can try the following and see ...READ MORE

answered Mar 1 in Python by SDeb
• 13,210 points
43 views