Pranjita Chakraborty
Pranjita Chakraborty

Reputation: 13

How to create a new list of tuples with existing list of tuples

I have a list of tuples like this -

list1 = [('alpha', 'beta'),
         ('beta','gama')
         ('alpha','lamda')
         ('gama', 'lamda'),
         ('euphor', 'tuphor')]

And I want to make a new list based upon the logic that -

for every pair which doesn't exist in the original list will be included in the new list, like the following:

new_list = [('alpha','gama'),
            (alpha, tuphor),
            (alpha, euphor),
            (beta,lamda),
            ()...]

likewise.

Can anyone suggest the method for doing so in python?

Thanks.

Upvotes: 0

Views: 98

Answers (4)

azro
azro

Reputation: 54148

You may

  • collect the different items
  • compute all the permutations
  • get the difference between all the combinations and the existing ones
list1 = [('alpha', 'beta'), ('beta', 'gama'), ('alpha', 'lamda'), ('gama', 'lamda'), ('euphor', 'tuphor')]
from itertools import chain, combinations

items = set(chain(*list1))  # {'euphor', 'gama', 'tuphor', 'beta', 'lamda', 'alpha'}

all_perm = set(combinations(items, r=2))
new_perm = all_perm - set(list1)

print(len(all_perm), all_perm)  # 30
print(len(new_perm), new_perm)  # 25

Upvotes: 0

Mohak Gangwani
Mohak Gangwani

Reputation: 104

You just need to get the unique names in the original list and then apply the if condition. Try this and let me know if you face any issue.

new_list = []
names = set(np.array(list1).ravel())
for i in names:
    for j in names:
        if i!=j:
            if ((i,j) not in list1) & ((j,i) not in list1) & ((i,j) not in new_list) & ((j,i) not in new_list):
                new_list.append((i,j))

Upvotes: 0

Roy2012
Roy2012

Reputation: 12493

Here's a solution using itertools and sets:

list1 = [('alpha', 'beta'),
         ('beta','gama'), 
         ('alpha','lamda'), 
         ('gama', 'lamda'),
         ('euphor', 'tuphor')]

all_items = set(itertools.chain(*list1))

all_pairs = set(itertools.product(all_items, all_items))
new_pairs = all_pairs.difference(list1)

The result (new_pairs) is:

{('alpha', 'alpha'),
 ('alpha', 'euphor'),
 ('alpha', 'gama'),
 ('alpha', 'tuphor'),
 ('beta', 'alpha'),
 ('beta', 'beta'),
 ('beta', 'euphor'),
 ('beta', 'lamda'),
 ('beta', 'tuphor'),
 ...

Upvotes: 1

Suraj
Suraj

Reputation: 2477

from itertools import combinations

list1 = [('alpha', 'beta'),
         ('beta','gama'),
         ('alpha','lamda'),
         ('gama', 'lamda'),
         ('euphor', 'tuphor')]

elements = list(set([e for l in list1 for e in l])) # find all unique elements

complete_list = list(combinations(elements, 2)) # generate all possible combinations

#convert to sets to negate the order

set1 = [set(l) for l in list1]
complete_set = [set(l) for l in complete_list]

# find sets in `complete_set` but not in `set1`
ans = [list(l) for l in complete_set if l not in set1]

Output :

[['euphor', 'lamda'],
 ['euphor', 'gama'],
 ['euphor', 'beta'],
 ['euphor', 'alpha'],
 ['lamda', 'beta'],
 ['lamda', 'tuphor'],
 ['gama', 'alpha'],
 ['gama', 'tuphor'],
 ['beta', 'tuphor'],
 ['tuphor', 'alpha']]

Upvotes: 1

Related Questions