-4

i had a list:

    list = ['a', 'b', 'c', 'd', 'e', 'f']

how would I go about converting this list to:

    list = ['ab', 'cd', 'ef']

Thanks in advance!

  • 5
    Please provide a [mcve] of your own code and explanation of what it is that is giving you issues in your current implementation. – idjaw Jun 14 '17 at 14:19
  • does the list always have an even number of elements? – depperm Jun 14 '17 at 14:19
  • How to group: Build always pairs? Build always 3 groups? What if the list isn't 6 elements in size? – Wolf Jun 14 '17 at 14:25
  • Also see https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks – PM 2Ring Jun 14 '17 at 14:28

3 Answers3

3

Given:

>>> li=['a', 'b', 'c', 'd', 'e', 'f']

You can use zip:

>>> ['{}{}'.format(a,b) for a,b in zip(li, li[1:])]
['ab', 'bc', 'cd', 'de', 'ef']

Or:

>>> [a+b for a,b in zip(li, li[1:])]
['ab', 'bc', 'cd', 'de', 'ef']

Or, if you want ['ab', 'cd', 'ef'] you can do:

>>> [a+b for a,b in zip(*[iter(li)]*2)]
['ab', 'cd', 'ef']

Or,

>>> [a+b for a,b in zip(li[::2],li[1::2])]
['ab', 'cd', 'ef']

For a different group length:

>>> [''.join(l) for l in zip(*[iter(li)]*3)]
['abc', 'def']

(And please don't use list as a name for a list. You clobber the function by the same name)

dawg
  • 80,841
  • 17
  • 117
  • 187
  • I hope this is not the starting point for a grouping strategies database ;) – Wolf Jun 14 '17 at 14:27
  • 1
    The desired output is `['ab', 'cd', 'ef']` and not `['ab', 'bc', 'cd', 'de', 'ef']` – Mohd Jun 14 '17 at 14:27
  • @MoeA What makes you sure about this? – Wolf Jun 14 '17 at 14:28
  • @Wolf its mentioned in the question? – Mohd Jun 14 '17 at 14:28
  • How would i be able to do the last one if i wasnt sure how many elements will be in the list? Automate it in a sense? – zan merc Jun 14 '17 at 14:37
  • @zanmerc: I don't follow your question. None of those is dependent on the number in the list. However, if the number in the list won't make a full pair such as `['a', 'b', 'c']` then the return is `['ab']` Would you want `['ab','c']`? – dawg Jun 14 '17 at 14:42
  • @dawg: sorry i wasnt clear, basically i need the groupings to be different depending on how many elements in the list. for example, if theres 4 elements i need to group them in two's or if there were 6 elements i'd need to group them in 3's. Would that be possible? – zan merc Jun 14 '17 at 14:47
  • @zanmerc: Yes. With `[a+b for a,b in zip(*[iter(li)]*2)]` Just have the `2` be a dynamic value that is set based on the length of the list. Such as `[a+b for a,b in zip(*[iter(li)]*len(li)/2]` – dawg Jun 14 '17 at 14:50
  • @dawg: everything works perfectly here, except for the "a+b for a,b" part, if the sets are based on length how can I avoid having to change the a+b part since the sets will not always be split into 2's? Thanks alot for your help by the way, sorry to bother you! – zan merc Jun 14 '17 at 15:08
  • Ahh -- got ya. Use `join`: `[''.join(l) for l in zip(*[iter(li)]*gr_len)]` – dawg Jun 14 '17 at 17:43
1
def pairs(sequence):
    sequence = iter(sequence)
    while True:
        yield next(sequence) + next(sequence)

>>> list(pairs(['a', 'b', 'c', 'd']))
['ab', 'cd']
>>> list(pairs(['a', 'b', 'c']))
['ab']

Note that it would ignore elements when the total number is not even.

Javier
  • 141
  • 8
0

You have many ways, here is one:

[n for n in map(lambda x: x[0]+x[1], zip(list[::2], list[1::2]))]

Reason I don't use list() to force output is because you are occupying it, which is not recommended. If you rename your list to ls, here is a shorter version:

list(map(lambda x: x[0]+x[1], zip(ls[::2], ls[1::2])))
hurturk
  • 4,726
  • 21
  • 37
  • Thanks for your help. i just tried this, and when i tried to print i got this: , how can i get it to actually print? – zan merc Jun 14 '17 at 14:26
  • I have updated for your case (python3). Usually you can call `list()` to force output but as you are already using it as your data variable, I used a different method of dumping. – hurturk Jun 14 '17 at 17:40