Python Forum
Converting List of 3 Element Tuple to Dictionary
Thread Rating:
  • 3 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting List of 3 Element Tuple to Dictionary
#11
(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!!!
Reply
#12
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)]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  removing one list element without using its index paul18fr 7 1,308 Feb-22-2025, 07:59 PM
Last Post: DeaD_EyE
  question about changing the string value of a list element jacksfrustration 4 2,236 Feb-08-2025, 07:43 AM
Last Post: jacksfrustration
  extract an element of a list into a string alexs 5 4,097 Aug-30-2024, 09:24 PM
Last Post: alexs
  Sort a list of dictionaries by the only dictionary key Calab 2 1,501 Apr-29-2024, 04:38 PM
Last Post: Calab
  element in list detection problem jacksfrustration 5 1,955 Apr-11-2024, 05:44 PM
Last Post: deanhystad
  using > < for tuple , list,... akbarza 3 1,832 Feb-05-2024, 01:18 PM
Last Post: deanhystad
  Dictionary in a list bashage 2 1,457 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 1,832 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  Searche each element of each tuple based 3 numbes zinho 8 2,334 Dec-11-2023, 05:14 PM
Last Post: zinho
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 1,722 Oct-17-2023, 09:46 AM
Last Post: tomciodev

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020