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.
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.
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.
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.
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 “.
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
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
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
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With