Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining pairs of elements of a list

I know that a list can be joined to make one long string as in:

x = ['a', 'b', 'c', 'd'] print ''.join(x) 

Obviously this would output:

'abcd' 

However, what I am trying to do is simply join the first and second strings in the list, then join the third and fourth and so on. In short, from the above example instead achieve an output of:

['ab', 'cd'] 

Is there any simple way to do this? I should probably also mention that the lengths of the strings in the list will be unpredictable, as will the number of strings within the list, though the number of strings will always be even. So the original list could just as well be:

['abcd', 'e', 'fg', 'hijklmn', 'opq', 'r']  
like image 819
John Avatar asked May 01 '11 20:05

John


People also ask

How do you join two elements in a list in python?

To join specific list elements (e.g., with indices 0 , 2 , and 4 ) and return the joined string that's the concatenation of all those, use the expression ''. join([lst[i] for i in [0, 2, 4]]) . The list comprehension statement creates a list consisting of elements lst[0] , lst[2] , and lst[4] .

How do I combine strings in a list?

Use the join() Method to Convert the List Into a Single String in Python. The join() method returns a string in which the string separator joins the sequence of elements. It takes iterable data as an argument. We call the join() method from the separator and pass a list of strings as a parameter.

What is pair () in python?

Pairs in Python. Page 1. Pairs in Python. To enable us to implement the concrete level of our data abstraction, Python provides a compound structure called a tuple, which can be constructed by separating values by commas. Although not strictly required, parentheses almost always surround tuples.


2 Answers

You can use slice notation with steps:

>>> x = "abcdefghijklm" >>> x[0::2] #0. 2. 4... 'acegikm' >>> x[1::2] #1. 3. 5 .. 'bdfhjl' >>> [i+j for i,j in zip(x[::2], x[1::2])] # zip makes (0,1),(2,3) ... ['ab', 'cd', 'ef', 'gh', 'ij', 'kl'] 

Same logic applies for lists too. String lenght doesn't matter, because you're simply adding two strings together.

like image 120
utdemir Avatar answered Sep 21 '22 13:09

utdemir


Use an iterator.

List comprehension:

>>> si = iter(['abcd', 'e', 'fg', 'hijklmn', 'opq', 'r']) >>> [c+next(si, '') for c in si] ['abcde', 'fghijklmn', 'opqr'] 
  • Very efficient for memory usage.
  • Exactly one traversal of s

Generator expression:

>>> si = iter(['abcd', 'e', 'fg', 'hijklmn', 'opq', 'r']) >>> pair_iter = (c+next(si, '') for c in si) >>> pair_iter # can be used in a for loop <generator object at 0x4ccaa8> >>> list(pair_iter)  ['abcde', 'fghijklmn', 'opqr'] 
  • use as an iterator

Using map, str.__add__, iter

>>> si = iter(['abcd', 'e', 'fg', 'hijklmn', 'opq', 'r']) >>> map(str.__add__, si, si) ['abcde', 'fghijklmn', 'opqr'] 

next(iterator[, default]) is available starting in Python 2.6

like image 41
kevpie Avatar answered Sep 21 '22 13:09

kevpie