Python – sort() vs sorted(list)

Posted 3 CommentsPosted in Coding, Python

You can compare list using sort() or sorted(list), but be careful with sort() – >>> c = [(‘d’,4), (‘c’,3), (‘a’,1), (‘b’, 2)] >>> a = [(‘a’,1), (‘b’, 2), (‘c’,3), (‘d’,4)] >>> a.sort() == c.sort() True >>> >>> a = [(‘a’,1), (‘b’, 2), (‘c’,3), (‘d’,4)] >>> b = [(‘b’,2), (‘c’, 3), (‘a’,1)] >>> >>> a.sort() == […]