How to extract from this list of tuples and convert into this dictionary?

user781486

I have this list of tuples.

List = [('id1', 'name1', 'date1'), ('id1', 'name2', 'date2'), ('id2', 'name3', 'date3'), ('id2', 'name4', 'date4')]

I want to have a function that converts list to become a dictionary such that

Dict = convert('id1', List)

Contents of Dict is {'id1':('name1', 'name2')}

The first parameter filters out the tuples that are of interest based on the first element. It is also the key of the output dictionary. The 2nd element of each tuple will become the values of the output dictionary.

I wrote a little function in Python and it does not work. Thank you very much for your help if you have better suggestions. Forget about my function if it is too far out which I think so. Solving it in a pythonic way is most welcome. I am using Python 2.7

def convertToDict(key, List):
    for key in List[0]:
        DictOut = {key:[]}
        DictOut[key].append(List[0])
thefourtheye

You are replacing the DictOut unconditionally. Instead you should be using setdefault like this

DictOut.setdefault(key, [])

Your program can be written like this

def convertToDict(my_list):
    result_dict = {}
    for item in my_list:
        result_dict.setdefault(item[0], []).append(item[1])
    return result_dict

print convertToDict(my_list)
# {'id2': ['name3', 'name4'], 'id1': ['name1', 'name2']}

If you want to get only the specific keys. you can use this

def convertToDict(key, my_list):
    result_dict = {}
    for item in my_list:
        if item[0] == key:
            result_dict.setdefault(item[0], []).append(item[1])
    return result_dict

print convertToDict("id1", my_list)
# {'id1': ['name1', 'name2']}

You can actually use collections.defaultdict for this purpose, like this

from collections import defaultdict
def convertToDict(key, my_list):
    result_dict = defaultdict(list)
    for item in my_list:
        if item[0] == key:
            result_dict[item[0]].append(item[1])
    return result_dict

print convertToDict("id1", my_list)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert this dictionary into a list of tuples?

Convert tuples to list of dictionary

Convert a list of tuples to a dictionary

Convert Dictionary to a list of Tuples

How can I convert a dictionary into a list of tuples?

How to convert a list of tuples to a dictionary of dictionaries in Python?

How to convert list of tuples to dictionary with index as key

How do I convert a list of tuples to a dictionary

How to extract list from list of tuples

Convert list of tuples into a dictionary with list of tuples

How to extract a list from a dictionary

How to create list of tuples from dictionary in python?

How to extract tuples from a list of lists in Haskell

how to extract Values from list of tuples python

How to extract only numbers from a list of tuples

How to extract the dictionary from list of dictionary with condition

Convert a nested dictionary into list of tuples

Convert nested dictionary into list of tuples

Convert dataframe to dictionary of list of tuples

Convert list of Tuples into Dictionary(All tuples convert seperate dictionary)

how to extract a value from an ordered dictionary of tuples, that changes order

How to convert list of key-value tuples into dictionary?

How to convert a list of 2-tuples into dictionary keys in Python?

How to convert a dictionary into a list of tuples without dict functions? [PYTHON]

Python: from list of tuples to dictionary of tuples

Convert list of tuples to named dictionary list

how to extract values from list of dictionary

How to extract the the key and values from list of dictionary?

How to create a list of tuples from the values of a dictionary of lists?