Bottom Page

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!!!
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  help with converting C# function to Python korenron 17 342 Yesterday, 10:26 AM
Last Post: Gribouillis
  Filter to List and/or Tuple conversion vindo 3 93 May-18-2019, 05:40 AM
Last Post: perfringo
  Iterate through a list of dictionary and append a new value. erina 1 115 May-16-2019, 09:55 AM
Last Post: perfringo
  Converting Program to OOP Carson147 2 131 May-13-2019, 07:28 PM
Last Post: rxndy
  Error while fetching data from PostgreSQL tuple indices must be integers or slices, n Sandy777 6 120 May-12-2019, 11:41 AM
Last Post: Sandy777
  Replace a list or string element with a random one.. pianistseb 3 76 May-09-2019, 08:24 AM
Last Post: buran
  Extract a List element NewBeie 4 139 May-03-2019, 09:49 AM
Last Post: NewBeie
  ValueError: dictionary update sequence element #0 has length 1; 2 Jmekubo 4 170 Apr-28-2019, 07:25 PM
Last Post: Jmekubo
  Convert List of Dictionary to dictionary of dictionary list in python kk230689 2 152 Apr-27-2019, 03:13 AM
Last Post: perfringo
  Converting List to Libray prophet11 6 262 Apr-22-2019, 04:49 AM
Last Post: perfringo

Forum Jump:


Users browsing this thread: 1 Guest(s)