0

I have this list:

[['abcde'],['defgh'],['dfsdf']]

And I want this result:

'abcdedefghdfsdf'

I want a simple solution.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
eoiiii
  • 9
  • 2
  • How is this not a duplicate after nearly 14 years and [1,942,974 Python questions](https://stackoverflow.com/questions/tagged/python)? Why do people keep answering the same beginner questions over and over and over again? – Peter Mortensen May 07 '22 at 13:15
  • `str(reduce(lambda x,y: "".join(["".join(x), "".join(y)]), [['abcde'],['defgh'],['dfsdf']]))` this is also good – Deepak Tripathi May 07 '22 at 13:17
  • Candidates: *[How do I concatenate items in a list to a single string?](https://stackoverflow.com/questions/12453580/how-do-i-concatenate-items-in-a-list-to-a-single-string)* (2012) and *[How to convert list to string](https://stackoverflow.com/questions/5618878/how-to-convert-list-to-string)* (2011). There must be plenty both before and after that. – Peter Mortensen May 07 '22 at 13:19

2 Answers2

5

Try this in one line:

"".join([i[0] for i in a])

It will loop over the items and turns them into ['abcde','defgh','dfsdf']. Then you will concatenate them with join.

So the result will be:

Out[4]: 'abcdedefghdfsdf'

Take a look at this link to learn more about .join().

As FreddyMcloughlan said in comments:

You could change the square brackets to parenthesis to turn it into a generator if working with very large lists

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59
-1

Try this snippet:

str_lists = [['abcde'],['defgh'],['dfsdf']]
output_str = "".join([lst.pop() for lst in str_lists])

"".join() is used to concatenate strings together from a list, while the inner list comprehension is used to flatten the list of lists into a single list

Ziggity
  • 23
  • 5