Python | Convert list of tuple into dictionary

Given a list containing all the element and second list of tuple depicting the relation between indices, the task is to output a dictionary showing the relation of every element from the first list to every other element in the list.
These type of problems are often encountered in Coding competition.
Below are some ways to achieve the above task.

Input:
indices = ['x','y','z','w','t','r']
relation =[('x', 'y'), ('x', 'z'), ('x', 'w'), ('y', 'z'), ('y', 'w'), ('z', 'w')]
Output:
{'x': ['y', 'z', 'w'], 'y': ['x', 'z', 'w'], 'z': ['x', 'y', 'w'], 
'w': ['x', 'y', 'z'], 't': [], 'r': []}

Method #1: Using iteration is the easiest way to solve any task

filter_none

edit
close

play_arrow

link
brightness_4
code

#Python code to convert list of tuple into dictionary showing 
#relation of every element from first list to every other element in the list.
  
#List initialization
indices = ['x','y','z','w','t','r']
relation =[('x', 'y'), ('x', 'z'), ('x', 'w'), ('y', 'z'), ('y', 'w'), ('z', 'w')]
  
#dictionary initialization
Output = {}
  
#Iteration 
for elem in indices:
     temp= []
     for rel in relation:
          if elem in rel:
               if elem == rel[0]:
                    temp.append(rel[1])
               else:
                    temp.append(rel[0])
       
     Output[elem] = temp
     temp = []
  
print("Initial list of tuple is :")
print(relation)
print("Converted dictionary of list :")
print(Output)

chevron_right


Output :
Initial list of tuple is :
[('x', 'y'), ('x', 'z'), ('x', 'w'), ('y', 'z'), ('y', 'w'), ('z', 'w')]
Converted dictionary of list :
{'w': ['x', 'y', 'z'], 'r': [], 'x': ['y', 'z', 'w'], 't': [], 
'y': ['x', 'z', 'w'], 'z': ['x', 'y', 'w']}

Method #2: Using networkx is the most simplest and shortest
way to convert list of tuple into dictionary



filter_none

edit
close

play_arrow

link
brightness_4
code

#Python code to convert list of tuple into dictionary showing 
#relation of every element from first list to every other element in the list.
  
#Importing
import networkx as nx 
  
#List initialization
indices = ['x','y','z','w','t','r']
relation =[('x', 'y'), ('x', 'z'), ('x', 'w'), ('y', 'z'), ('y', 'w'), ('z', 'w')]
  
#dictionary initialization
Output = {}
  
#Using networkx to solve 
temp=nx.Graph(relation)
temp.add_nodes_from(indices)
Output = nx.to_dict_of_lists(temp)
  
#Printing
print("Initial list of tuple is :")
print(relation)
print("Converted dictionary of list :")
print(Output)

chevron_right


Output :
Initial list of tuple is :
[('x', 'y'), ('x', 'z'), ('x', 'w'), ('y', 'z'), ('y', 'w'), ('z', 'w')]
Converted dictionary of list :
{'w': ['x', 'y', 'z'], 'r': [], 'x': ['y', 'z', 'w'], 't': [], 
'y': ['x', 'z', 'w'], 'z': ['x', 'y', 'w']}

Method #3: Using itertools and groupby is another way to convert list of tuple into dictionary.

filter_none

edit
close

play_arrow

link
brightness_4
code

#Python code to convert list of tuple into dictionary showing 
#relation of every element from first list to every other element in the list.
  
#Importing
from itertools import groupby
from operator import itemgetter
  
#List initialization
indices = ['x','y','z','w','t','r']
relation =[('x', 'y'), ('x', 'z'), ('x', 'w'), ('y', 'z'), ('y', 'w'), ('z', 'w')]
  
#Using itertools.groupby and maps
edge = relation + [tuple(reversed(pair)) for pair in relation]
st = itemgetter(0)
end = itemgetter(1)
groups = groupby(sorted(edge), key=st)
mapping = {vertex: list(map(end, edges)) for vertex, edges in groups}
from collections import defaultdict
  
#Output list
Output = defaultdict(list, mapping)
Output = dict(mapping)
Output.update({vertex: [] for vertex in indices if vertex not in mapping})
  
#Printing
print("Initial list of tuple is :")
print(relation)
print("Converted dictionary of list :")
print(Output)

chevron_right


Output :
Initial list of tuple is :
[('x', 'y'), ('x', 'z'), ('x', 'w'), ('y', 'z'), ('y', 'w'), ('z', 'w')]
Converted dictionary of list :
{'w': ['x', 'y', 'z'], 'r': [], 'x': ['y', 'z', 'w'], 't': [], 
'y': ['x', 'z', 'w'], 'z': ['x', 'y', 'w']}


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.