1

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?

5
  • list is an inbuilt function, you should give your variable a different name Commented Apr 12, 2019 at 8:12
  • works for me: >>> [":".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'] Commented Apr 12, 2019 at 8:13
  • it works for me, too Commented Apr 12, 2019 at 8:13
  • what's your list look like? Commented Apr 12, 2019 at 8:14
  • 2
    @Sayse list is an inbuilt function not a keyword Commented Apr 12, 2019 at 8:15

1 Answer 1

1

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']
Sign up to request clarification or add additional context in comments.

1 Comment

There is no longer an issue. Thank you. I got that my solution was in fact correct, but I was passing wrong arguments which made to split string every single character. (I have 2 strings abcdefghijkl which I split to what's shown in the topic, but to join them I was using non split one). Beginners mistakes right..?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.