Python | Convert a list into tuple of lists
We are given a list, the task is to convert the list into tuple of lists.
Input: ['Geeks', 'For', 'geeks'] Output: (['Geeks'], ['For'], ['geeks'])
Input: ['first', 'second', 'third'] Output: (['first'], ['second'], ['third'])
Method #1: Using Comprehension
Python3
# Python code to convert a list into tuple of lists # Initialisation of list Input = [ 'Geeks' , 'for' , 'geeks' ] # Using list Comprehension Output = tuple ([name] for name in Input ) # printing output print (Output) |
Output:
(['Geeks'], ['for'], ['geeks'])
Method #2 : Using Map + Lambda
Python3
# Python code to convert a list into tuple of lists # Initialisation of list Input = [ 'first' , 'second' , 'third' ] # Using map + lambda Output = tuple ( map ( lambda x: [x], Input )) # printing output print (Output) |
Output:
(['first'], ['second'], ['third'])
Method #3 : Using Map + zip
Python3
# Python code to convert a list into tuple of lists # Initialisation of list Input = [ 'first' , 'second' , 'third' ] # Using Map + zip Output = tuple ( map ( list , zip ( Input ))) # printing output print (Output) |
Output:
(['first'], ['second'], ['third'])
Method #4: Using enumerate function
Python3
lst = [ 'Geeks' , 'for' , 'geeks' ] x = tuple ([i] for a,i in enumerate (lst)) print (x) |
Output
(['Geeks'], ['for'], ['geeks'])
Please Login to comment...