Rakesh - 1 month ago 4x

Python Question

I have a list

`[ 'a' , 'b' , 'c' , 'd']`

`[ 'ab', 'bc' , 'cd']`

Answer

Use `zip`

within a list comprehension:

```
In [13]: ["".join(seq) for seq in zip(lst, lst[1:])]
Out[13]: ['ab', 'bc', 'cd']
```

Or since you just want to concatenate two character you can also use `add`

operator, by using `itertools.starmap`

in order to apply the add function on character pairs:

```
In [14]: from itertools import starmap
In [15]: list(starmap(add, zip(lst, lst[1:])))
Out[15]: ['ab', 'bc', 'cd']
```

Source (Stackoverflow)

Comments