Search code examples
pythonlistdictionarydata-structuresdefaultdict

data structure to store unique id on particular date


I have data like date,id,v_id. I want a data structure to represent this data in such a way that one v_id will be there on a particular date, it should not repeat.

Which data structure should I choose? I thought of using default dict. like in below format.

{date1:[{v_id1:id1},{v_id2:id2},{v_id3:id3}],date2:[{v_id1:id1},{v_id2:id2},{v_id3:id3}]}

If I'm thinking in a correct way, then how should I implement this? and if there is any other better idea, please most welcome.


Solution

  • To make the v_id unique for each date, you should use some sort of hashtable for the nested data structure, which is the underlying data structure for a dictionary:

    {date1: {v_id1: id1, v_id2: id2, v_id3: id3}...}
    

    Once a date is known, a v_id (or id given the v_id) can be found in O(1); you can hardly beat that.

    Your actual implementation will look sometime like:

    from collections import defaultdict
    
    d = defaultdict(dict)