#!/usr/bin/env python
# -*- coding: utf-8 -*-

my_list = [(0,1),(1,0),(2,1),(3,0),(4,1),(5,3),(6,3)]

# remove the comment to sort the list by id if you need
#my_list.sort()

def select(my_list, k):

    # used to check the sum of two value equals to k
    chk = {}

    # used to store the candidates which pass the check with the data structure (item_1, item_2)
    tmp = []
    for item in my_list:

        if k - item[1] in chk and chk[k - item[1]]:
            tmp.append((chk[k - item[1]].pop(), item))
        else:
            if item[1] in chk:
                chk[item[1]].append(item)
            else:
                chk[item[1]] = [item]

    reminder = [item for value in chk.values() for item in value]
    return tmp, reminder

B, my_list = select(my_list, 2)
C, D = select(my_list, 1)

print "B =", B
print "C =", C
print "D =", D