1

due to some problems with lemmatization I had to import one list in the form of:

list_a = [['a', 'b'], ['c', 'd'], ['a', 'b']]

Now I want to count the frequency of each elements, but the list is unhashable in this form. Therefore I want to convert it into:

list_b = ['a b', 'c d', 'a b']]

I know that this is a very self-made problem and probably a dirty workaround, but in the imported form of list_b the lemmatization only lemmatized single-elements of the list and did not touch any composed elements.

Thanks a lot for your time.

2 Answers2

3

You can use a list comprehension with string concatenation:

list_a = [['a', 'b'], ['c', 'd'], ['a', 'b']]

list_b = [x + ' ' + y for x, y in list_a]

You can also use str.join (useful if there are more than two elements in the sublists):

list_b = [' '.join(a) for a in list_a]

Output:

['a b', 'c d', 'a b']
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
1

You can also use map function:

list_a = [['a', 'b'], ['c', 'd'], ['a', 'b']]

list_b = list(map(' '.join, a))
# ['a b', 'c d', 'a b']

Understanding the map function

Parham.rm
  • 133
  • 7