daniel_dezan
daniel_dezan

Reputation: 53

To concatenate an array inside other array?

I want to concatenate an array inside another array.

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

Output: ['ab', 'cd']

Input = [['a', 'b'], ['c', 'd'], ['f', '.']]

Output: ['ab', 'cd', 'f.']

Conditions:

Upvotes: 0

Views: 349

Answers (3)

PyotrVanNostrand
PyotrVanNostrand

Reputation: 248

L = [["a", "b", "c"],["c","d"], ["x", "y", "z"]]

newL = []

for i in L:
    newL.append("".join(i))

print(newL)

This is what you need for.

Upvotes: 0

Alain T.
Alain T.

Reputation: 42133

You could use map to apply a join to all the sublists:

[*map(''.join,Input)]


['ab', 'cd']

Upvotes: 0

T1Berger
T1Berger

Reputation: 505

Don't be decourage as a beginner, asking the right question in SO is rly diffcult - here is a simple list comprehension solution with a string join:

input = [["a","b"], ['c','d']]

[''.join(list) for list in input]

Output: ['ab', 'cd']

As suggested by @ChaddRobertson here is the same solution with an easier to understandable for-each loop:

ouput_list = []
for list in input:
    ouput_list.append(''.join(list))
    
print(ouput_list)

Upvotes: 2

Related Questions