How to merge the elements in a list sequentially in python -


i have list [ 'a' , 'b' , 'c' , 'd']. how list joins 2 letters sequentially i.e ouptut should [ 'ab', 'bc' , 'cd'] in python instead of manually looping , joining

use zip within list comprehension:

in [13]: ["".join(seq) seq in zip(lst, lst[1:])] out[13]: ['ab', 'bc', 'cd'] 

or since want concatenate 2 character can use add operator, using itertools.starmap in order apply add function on character pairs:

in [14]: itertools import starmap  in [15]: list(starmap(add, zip(lst, lst[1:]))) out[15]: ['ab', 'bc', 'cd'] 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -