0

I'm trying to figure out a way to split a list of tuples into pairs. This should work for a list that is always of an even number.

I'm drawing a bit of a blank on how to do this and hope someone can point me in the right direction.

data =  [(1, 'name1'), (2, 'name2'), (3, 'name3'), (4, 'name4')]

should end up in something like:

set1 = [(1, name1, 2, name2)]
set2 = [(3, name3, 4, name4)]

How can I achieve this? Docs to read or the like?

Cœur
  • 37,241
  • 25
  • 195
  • 267
mnickey
  • 727
  • 1
  • 6
  • 15
  • Look at the grouper recipe on [itertools](https://docs.python.org/2/library/itertools.html). Maybe combine that with chain. – kojiro Mar 18 '15 at 18:43
  • 1
    possible duplicate of [How do you split a list into evenly sized chunks in Python?](http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python) – squiguy Mar 18 '15 at 18:44
  • 1
    Hmm, a question asking how to split a list into evenly sized chunks would be a dupe. This question could be _answered_ using, in part, some knowledge from that other question, but it's clearly not a dupe. – kojiro Mar 18 '15 at 18:45

7 Answers7

1

Simplest would be to make an iterator over the list, and zip that iterator with itself; then sum the pairs together in a list comprehension.

>>> data = [(1, 'name1'), (2, 'name2'), (3, 'name3'), (4, 'name4')]
>>> iterator = iter(data)
>>> [ i + j for i, j in zip(iterator, iterator) ]
[(1, 'name1', 2, 'name2'), (3, 'name3', 4, 'name4')]

or as an oneliner

[ i + j for i, j in zip(*[iter(data)] * 2) ]
0

Using the itertools grouper recipe:

import itertools

def grouper(iterable, n, fillvalue=None):
    groups = [iter(iterable)]*n
    return itertools.zip_longest(*groups, fillvalue=fillvalue)

And chain them together.

data = [(1, 'name1'), (2, 'name2'), (3, 'name3'), (4, 'name4')]
result = [tuple(itertools.chain.from_iterable(group)) for group in grouper(data, 2)]

# result is:

[(1, 'name1', 2, 'name2'),
 (3, 'name3', 4, 'name4')]
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
0

Simple solution:

def split(tupls):
if len(tupls) % 2 != 0:
    print("list's length must be even")
return [[tupls[2 * i][0], tupls[2 * i][1], tupls[2 * i + 1][0], tupls[2 * i  + 1][1]] for i in range(len(tupls) // 2)]
Mikhail
  • 395
  • 3
  • 17
0

To divide the list in two just get the halfway point in the list and use itertools.chain.from_iterable to chain the elements from each half into a single tuple.

from itertools import chain, islice

data =  [(1, 'name1'), (2, 'name2'), (3, 'name3'), (4, 'name4')]
half = len(data)//2

set1 = [tuple(chain.from_iterable(islice(data, None, half)))]
set2 = [tuple(chain.from_iterable(islice(data, half, None)))]


print(set1)
print(set2)
[(1, 'name1', 2, 'name2')]
[(3, 'name3', 4, 'name4')]

If you want actually want a list of pairs because your lists are always even you can use iter and next:

it = iter(data)
pairs  =  [ele + next(it) for ele in it]
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

You can try something like this.

data =  [(1, 'name1'), (2, 'name2'), (3, 'name3'), (4, 'name4')] #even number
data = [data[i:i+2] for i in range(0, len(data), 2)] #where 2 is the number of tuples in the list
print data
data =  [(1, 'name1'), (2, 'name2'), (3, 'name3'), (4, 'name4'), (5, 'name5')]
data = [data[i:i+2] for i in range(0, len(data), 2)] #odd number
print data
reticentroot
  • 3,612
  • 2
  • 22
  • 39
0

you can try by this:

middle = len(data)/2
set1 = [tuple([data[i][j] for i in range(0,middle)for j in range(2)])]
set2 = [tuple([data[i][j] for i in range(middle, len(data))for j in range(2)])]
Sakib Ahammed
  • 2,452
  • 2
  • 25
  • 29
0
from itertools import chain

data =  [(1, 'name1'), (2, 'name2'), (3, 'name3'), (4, 'name4')]

for i in range(0, len(data), 2):
     pair = tuple(chain(*data[i:i+2]))

or a one liner list of pairs

pairs = [tuple(chain(*data[i:i+2])) for i in range(0, len(data), 2)]
Jacob Burbach
  • 506
  • 5
  • 4