Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can´t convert a list with sublists that have 2 elements in each of them to a single string

So, I need to convert a list with sublists that have 2 elements in each of them to a single string

What I have:

[['A','B'],['C','D']]

What´s what I want to be converted to:

"ABCD"

I tried this:

list=[['A','B'],['C','D']]

hello=""

for i in list:

     hello=hello+i

print (hello)

Says that I have a TypeError, and I can´t understand why.

like image 262
Vasco Figueiroa Avatar asked Dec 03 '16 10:12

Vasco Figueiroa


People also ask

How to Convert a list of strings into a single string?

To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.

What is the difference between a list and a sublist?

The data structure we're using in this example is called a list. The difference between a sentence and a list is that the elements of a sentence must be words, whereas the elements of a list can be anything at all: words, #t , procedures, or other lists. (A list that's an element of another list is called a sublist.

How to join all elements in a list Python?

Python String join() The string join() method returns a string by joining all the elements of an iterable (list, string, tuple), separated by the given separator.

How to return a list of strings in Python?

A Python function can return any object such as a list. To return a list, first create the list object within the function body, assign it to a variable your_list , and return it to the caller of the function using the keyword operation “ return your_list “.


4 Answers

You have a list of iterable so as a pythonic way, use itertools.chain.from_iterable to flatten your list then join the characters with str.join() method:

In [12]: from itertools import chain

In [13]: lst = [['A','B'],['C','D']]

In [14]: ''.join(chain.from_iterable(lst))
Out[14]: 'ABCD'

Here is a benchmark against using two join, which shows that the itertools approach is 2+ times faster:

In [19]: %timeit ''.join(chain.from_iterable(lst))
10000 loops, best of 3: 114 µs per loop

In [20]: %timeit ''.join(''.join(w) for w in lst)
1000 loops, best of 3: 250 µs per loop
like image 137
Mazdak Avatar answered Nov 07 '22 17:11

Mazdak


Says that I have a TypeError, and I can´t understand why

The error you got is pretty verbose: TypeError: Can't convert 'list' object to str implicitly, that means that you can't implicitly concatenate a list object with a string object like [1,2] + "hello"

On the other hand, you can concatenate lists, and in your simple case this ''.join(list[0]+list[1]) will also give the expected result ABCD

Use str.join(iterable) function(or join with itertools.chain.from_iterable function which should go faster):

l = [['A','B'],['C','D']]
hello = ''.join(''.join(w) for w in l)

print(hello)

The output:

ABCD

https://docs.python.org/3.5/library/stdtypes.html?highlight=join#str.join

like image 39
RomanPerekhrest Avatar answered Nov 07 '22 17:11

RomanPerekhrest


try this

sampleData = [['A','B'],['C','D']]
output = "".join( "".join(i) for i in sampleData)
print (output)

or

sampleData = [['A','B'],['C','D']]
output = ""
for i in sampleData:
    output += "".join(i)
print (output)

output

ABCD

The error you are getting becouse you are trying to add a list and a string

like image 29
Mr. A Avatar answered Nov 07 '22 17:11

Mr. A


You can do it while only calling str.join() once and using a list comprehension:

''.join([char for element in [['A', 'B'], ['C', 'D']] for char in element])

Outputs:

'ABCD'
like image 37
Pierre Barre Avatar answered Nov 07 '22 17:11

Pierre Barre