Python Forum
convert a named tuple to a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert a named tuple to a dictionary
#1
is there a better way (like a built-in way) to convert a named tuple to a dictionary?

def named_tuple_to_dict(t):
    return {n:getattr(t,n) for n in dir(t) if n[0] != '_'}
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
convert a named tuple to a dictionary - by Skaperen - Mar-30-2022, 12:31 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to convert tuple value into string mg24 2 4,271 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  convert List with dictionaries to a single dictionary iamaghost 3 4,091 Jan-22-2021, 03:56 PM
Last Post: iamaghost
Sad Convert python list to dictionary akanowhere 6 4,704 Dec-27-2020, 09:26 PM
Last Post: Pedroski55
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 4,658 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Updating dictionary with tuple Mark17 2 2,783 Aug-06-2020, 02:59 PM
Last Post: Mark17
  how to get the keys in a named tuple Skaperen 5 14,303 Mar-25-2020, 09:51 PM
Last Post: Skaperen
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 4,530 May-21-2019, 11:39 AM
Last Post: avorane
  Convert List of Dictionary to dictionary of dictionary list in python kk230689 2 82,620 Apr-27-2019, 03:13 AM
Last Post: perfringo
  Converting List of 3 Element Tuple to Dictionary fooikonomou 11 8,094 Jan-14-2019, 09:51 AM
Last Post: perfringo
  unable to convert text file in dictionary purnima1 6 5,704 Apr-02-2018, 07:44 AM
Last Post: purnima1

Forum Jump:

User Panel Messages

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