Python: Create set by removing duplicates in text processing?
Question: Let's say a text file with two columns like below

    
    
    A "
    A "
    A l
    A "
    C r
    C "
    C l
    D a
    D "
    D "
    D "
    D d
    R "
    R "
    R "
    R " 
    S "
    S "
    S o
    D g
    D "
    D "
    D "
    D j
    A "
    A "
    A z
    

I would like retrieve the information like below

    
    
    list1= {A:l}, {C:r,l}, {D:a,d}, {S:o}
    final_list= {A:l}, {C:r,l}, {D:a,d}, R{}, {S:o}
    

I understand that , I have to access the text file `line.strip().split()`

and after that I don't know how to proceed.


Answer: 
    import collections
    list1 = collections.defaultdict(set)
    final_list = collections.defaultdict(set)
    for line in filetext: ## assuming youve opened it, read it in
        key, value = line.strip().split()
        final_list[key].add(value)
        if value != '"':
            list1[key].add(value)
    

This is slightly different in that `final_list` will have the empty string as
an element; this doesn't match what you said, so let's alter it a little:

    
    
    import collections
    list1 = collections.defaultdict(set)
    final_list = {}
    for line in filetext: ## assuming youve opened it, read it in
        key, value = line.strip().split()
        if key not in final_list:
            final_list[key] = set()
        if value != '"':
            list1[key].add(value)
    final_list.update(list1)
    

This should give you what you want - existence with empty-sets for things like
`R`.

