Donaatas
Donaatas

Reputation: 13

Joining python string from list by using .join function is not outputing wanted result

EDIT: to clarify I changed list name for this question and I don't call it "list" in my code. It's called for what it represents, but that is not important in this topic.

I have the following list:

[['ab', 'cd', 'ef', 'gh', 'ij', 'kl'],
 ['ab', 'cd', 'ef', 'gh', 'ij', 'kl'],
 ['ab', 'cd', 'ef', 'gh', 'ij', 'kl'],
 ['ab', 'cd', 'ef', 'gh', 'ij', 'kl'],
 ['ab', 'cd', 'ef', 'gh', 'ij', 'kl'],
 ['ab', 'cd', 'ef', 'gh', 'ij', 'kl']]

and I need to connect them as:

ab:cd:ef:gh:ij:kl

Tried using for loop and join function:

for i in list:
        connect.append(':'.join(i))

But this gets me to:

a:b:c:d:e:f:g:h:i:j:k:l

Can anyone help me clarify what is wrong in my script?

Upvotes: 1

Views: 73

Answers (1)

Netwave
Netwave

Reputation: 42678

Your code should work, anyway here you have a comprehension achieving the same:

>>> l = [['ab', 'cd', 'ef', 'gh', 'ij', 'kl'],
...  ['ab', 'cd', 'ef', 'gh', 'ij', 'kl'],
...  ['ab', 'cd', 'ef', 'gh', 'ij', 'kl'],
...  ['ab', 'cd', 'ef', 'gh', 'ij', 'kl'],
...  ['ab', 'cd', 'ef', 'gh', 'ij', 'kl'],
...  ['ab', 'cd', 'ef', 'gh', 'ij', 'kl']]
>>> [":".join(x) for x in l]
['ab:cd:ef:gh:ij:kl', 'ab:cd:ef:gh:ij:kl', 'ab:cd:ef:gh:ij:kl', 'ab:cd:ef:gh:ij:kl', 'ab:cd:ef:gh:ij:kl', 'ab:cd:ef:gh:ij:kl']

Upvotes: 1

Related Questions