Python – Convert String Records to Tuples Lists
Sometimes, while working with data, we can have problem in which we need to convert the data list which in string format to list of tuples. This can occur in domains in which we have cross type inputs. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop +
The combination of above methods can be used to solve this task. In this we remake the string after processing for input to eval function to convert to Tuple lists.
The original string is : [(gfg, ), (is, ), (best, )] The list of Tuples is : [('gfg', ), ('is', ), ('best', )]
Method #2 : Using regex + list comprehension
The combination of above functionalities is used to perform this task. In this, we employ regex function to perform the task of resolving the String and list comprehension helps in reconstruction of record list.
The original string is : [(gfg, ), (is, ), (best, )] The list of Tuples is : [('gfg', ''), ('is', ''), ('best', '')]