Python Forum
Converting List of 3 Element Tuple to Dictionary - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Converting List of 3 Element Tuple to Dictionary (/thread-15255.html)

Pages: 1 2


RE: Converting List of 3 Element Tuple to Dictionary - fooikonomou - Jan-11-2019

(Jan-11-2019, 04:27 PM)DeaD_EyE Wrote: result = []
for element in data:
    sorted_tuple = sorted(element)
    result.append(sorted_tuple)
print(result)
It worked Many thanks!!!


RE: Converting List of 3 Element Tuple to Dictionary - perfringo - Jan-14-2019

Based on DeaD_EyE code we can build one-liner list comprehension that sorts both elements and list:

>>> data = [(1, 5, 1), (1, 3, 2), (1, 3, 1), (1, 1, 4)]
>>> sorted([tuple(sorted(element)) for element in data])
[(1, 1, 3), (1, 1, 4), (1, 1, 5), (1, 2, 3)]