1
l = ["Youtube", "Instagram", "Facebook"]

a = [
    [
        "{} {}".format(i[2 * j : 2 * j + 1], i[2 * j + 1 : 2 * j + 2])
        for j in range(len(i) // 2)
    ]
    for i in l
]
print(a)

This will return

[['Y o', 'u t', 'u b'], ['I n', 's t', 'a g', 'r a'], ['F a', 'c e', 'b o', 'o k']]

How the above list I can join first character space with the second character, Please tell if i missing anything in the inner array List.

Below is the output I want the odd space with even character

[['Yuu otb'], ['Isar ntga'], ['Fcbo aeok']]
3
  • And you want each of them as a list with one element? Commented Oct 1, 2019 at 12:44
  • And you want to discard any final odd letter, correct? I.e. the "e" in "Youtube" and the "m" in "Instagram"? Commented Oct 1, 2019 at 12:47
  • What have you tried so far? You have pretty good code in here, you don't need to join the elements, just modify how the inner part works. Commented Oct 1, 2019 at 12:48

4 Answers 4

4

You could join slices of the strings as follows:

[[' '.join((s[:-1:2], s[1::2]))] for s in l]
# [['Yuu otb'], ['Isar ntga'], ['Fcbo aeok']]
Sign up to request clarification or add additional context in comments.

3 Comments

Slickest solution so far (IMHO).
@yatu, you are a man, how you think in that way one-liner solution, I really appreciate your solution thanks!
@yatu the first slice should be s[::2] because it will go untill the end of the string, it fails for l = ["ivvkxq", "ivvkx"], So please edit your awswer
1

You don't actually need a double list comp for this. You can accomplish this by slicing up to the last even element and stepping by 2.

If you want them as single strings in a one-deep list:

[f'{x[:2*(len(x)//2):2]} {x[1:2*(len(x)//2):2]}' for x in l]
# returns:
['Yuu otb', 'Isar ntga', 'Fcbo aeok']

If you want them 2-deep:

[[f'{x[:2*(len(x)//2):2]} {x[1:2*(len(x)//2):2]}'] for x in l]
# returns:
[['Yuu otb'], ['Isar ntga'], ['Fcbo aeok']]

Comments

0

Sticking with the main structure of your original solution,

a = [
    [
        "{} {}".format("".join([i[2 * j : 2 * j + 1] for j in range(len(i) // 2)]),
            "".join([i[2 * j + 1 : 2 * j + 2] for j in range(len(i) // 2)]))
    ]
    for i in l
]

2 Comments

Scott Hunterr, I agree with your solution will give desire output, but this seems to be take high time complexity as there are 3*for loop
Time is comparable to your original code; additional loop isn't nested. Your code did 2 expressions for each j; mine does 1 expression for each j, twice.
0

I have combined zip and join

l = ["Youtube", "Instagram", "Facebook"]
a = [["{} {}".format(i[2 * j : 2 * j + 1], i[2 * j + 1 : 2 * j + 2])for j in range(len(i) // 2)]for i in l]
b = [[''.join(x) for x in zip(*i) if ''.join(x).replace(' ', '')] for i in a]
c = [' '.join(i) for i in b]
print(c)

Output:

['Yuu otb', 'Isar ntga', 'Fcbo aeok']

Comments

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.