for example

lst = [['pen','table'],['pen','apple'],['table','morning']] 

Mean what I need to do? And I need each item in the list of lists to become the key to the list of items with which it is paired.

 lst = {'pen': ['table','apple'], 'table': ['pen','morning'], 'apple': ['pen'], 'morning': ['table']} 

    3 answers 3

     lst = [['pen', 'table'], ['pen', 'apple'], ['table', 'morning']] result = {} for level1 in lst: for level2 in level1: elements = list(level1) elements.remove(level2) result[level2] = result.get(level2, []) + elements print(result) {'morning': ['table'], 'table': ['pen', 'morning'], 'pen': ['table', 'apple'], 'apple': ['pen']} 
    • Thank you very much.) But if it is not difficult for you, could you tell us more about the last line. - Bernard
    • @Bernard The last line is the output of the result :) And if you are about result [level2] = result.get (level2, []) + elements, then an element in the dictionary is searched for in the right part or an empty list is created if there is such a word in the dictionary keys not yet. After that, elements of the same list are added to the resulting list (empty or not), but without the word itself. And after that, the dictionary is assigned with the same key. - Jenyay
    • Thanks again)) - Bernard

    In such cases, it is convenient to use collections.defaultdict :

     from collections import defaultdict d = defaultdict(list) for a, b in lst: d[a].append(b) d[b].append(a) 

    defaultdict by itself as a dictionary, but if you need an exact type, you can call dict(d) .

    Result

     {'apple': ['pen'], 'morning': ['table'], 'pen': ['table', 'apple'], 'table': ['pen', 'morning']} 
    • An elegant solution ... - MaxU

    As an additional option without using defaultdict , you can use the dict.setdefault method with a similar effect:

     d = {} for a, b in lst: d.setdefault(a, []).append(b) d.setdefault(b, []).append(a) 

    The setdefault(key, default) method takes the key key , the default value is default and returns the value by the key key . In case there was no such key, it additionally adds the key key with the transmitted value of default . Its approximate code is as follows:

     class dict: def setdefault(self, key, default): if key not in self: self[key] = default return self[key] 

    The disadvantages of this solution are as follows:

    • Empty lists are created every time a key is already in the dictionary.
    • The setdefault method has an obscure name.

    However, here we are using standard dictionary functionality.


    Additionally, if you want to maintain the uniqueness of elements by key (for example, in case pairs can repeat with the same or inverse order), you should use set :

     lst = [['pen', 'table'], ['pen', 'apple'], ['apple', 'pen'], ['table', 'morning']] d = {} for a, b in lst: d.setdefault(a, set()).add(b) d.setdefault(b, set()).add(a) 

    Or, for a solution with defaultdict :

     from collections import defaultdict d = defaultdict(set) for a, b in lst: d[a].add(b) d[b].add(a) 

    In these cases, the d dictionary will look like this:

     {'apple': {'pen'}, 'morning': {'table'}, 'pen': {'apple', 'table'}, 'table': {'morning', 'pen'}} 

    instead

     {'apple': ['pen', 'pen'], 'morning': ['table'], 'pen': ['table', 'apple', 'apple'], 'table': ['pen', 'morning']}