It is not possible to convert a list with sublists that have 2 items in each of them into one line

So, I need to convert a list with sublists that have 2 elements in each of them into one line

What I have:

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

What I want to convert to:

 "ABCD" 

I tried this:

 list=[['A','B'],['C','D']] hello="" for i in list: hello=hello+i print (hello) 

Says I have a TypeError and I can’t understand why.

+5
source share
5 answers

You have a list of iterations, like pythonic, use itertools.chain.from_iterable to flatten your list, then str.join() characters using the 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's an example of a comparison using two join , which show 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 
+4
source

Says I have a TypeError and I can't figure out why

You got pretty verbose: TypeError: It is not possible to convert the list object to str implicitly, this means you cannot implicitly combine the list object with a string object, for example [1,2] + "hello"

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

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

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

Output:

 ABCD 

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

+3
source

try it

 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 get because you are trying to add a list and a string

+2
source

You can only do this when you call str.join() once and using list comprehension:

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

Outputs:

 'ABCD' 
+2
source

The TypeError function outputs your operation to add the substring i to the welcome line. I believe this is due to the fact that the operation is not supported between the type list and str, as can be seen from the following examples:

 ['a', 'b']+'2' Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: can only concatenate list (not "str") to list '2'+['a', 'b'] Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: Can't convert 'list' object to str implicitly 

There are various answers above that recommend alternative code and use the "join" method. However, although I would include an alternative that uses pure loops:

 l = [['A','B'],['C','D']] hello = "" for sublist in l: for element in sublist: hello += str(element) print(hello) 

Note that this is probably slower than other methods. In addition, I recommend that you do not use the “list” as the variable name, as it is already used by the class.

0
source

Source: https://habr.com/ru/post/1260754/


All Articles