Home > Blockchain >  Convert a list to a string in another list
Convert a list to a string in another list

Time:12-07

I am working on the lists but I had a problem. There is a list as follows:

First_last_Name = [['hassan','abasi'],['mohammad','sajadi'],['bahman','mardomani'],['sarah','masti']]

Now we want to convert it as follows:

First_last_Name = ['hassan abasi','mohammad sajadi','bahman mardomani','sarah masti']

I want to try to write it in one line with lambda.

The code I am trying to apply in one line is as follows:

full_str = [(lambda x: str(x))(x) for x in First_last_Name]

output :

["['hassan', 'abasi']",
 "['mohammad', 'sajadi']",
 "['bahman', 'mardomani']",
 "['sarah', 'masti']"]

But I can't delete that internal list and turn it into a string.

CodePudding user response:

You can do it with a list comprehension and join

result = [" ".join(i) for i in First_last_Name]

Result:

['hassan abasi', 'mohammad sajadi', 'bahman mardomani', 'sarah masti']

CodePudding user response:

First_last_Name = [['hassan','abasi'],['mohammad','Sajadi'],['bahman','mardomani'],['sarah','masti']] 
  
new_list = [ " ".join(i) for i in First_last_Name]

CodePudding user response:

I'd prefer @sembei-norimaki 's solution. But as you asked about lambda:

result = list(map(lambda x: ' '.join(x), First_last_Name))

or even simpler:

result = list(map(' '.join, First_last_Name))
  • Related