Finding nested list with identical values in Dictionaries [Python]

William Merritt

Currently have a Dictionary that is structured as such

data =
{
     "Invitees": [
    {
           "firstName": "Will",
           "lastName": "Klein",
           "email": "WKlein@***il.com",
           "highSchool": "NorthwestHS",
           "availableDates": [
               "2018-02-01",
               "2018-02-02"
           ]
     },
     {
           "firstName": "Jack",
           "lastName": "Smith",
           "email": "JSmith@***il.com",
           "highSchool": "SouthwestHS",
           "availableDates": [
                "2018-02-04",
                "2018-01-01"
            ]
     },
     {
           "firstName": "Jamie",
           "lastName": "Farough",
           "email": "JFarough@***il.com",
           "highSchool": "NorthwestHS",
           "availableDates": [
                "2018-02-01",
                "2018-02-02"
           ]
     },
     {
           "firstName": "Bob",
           "lastName": "Saggot",
           "email": "BSaggot@***il.com",
           "highSchool": "NorthwestHS",
           "availableDates": [
               "2018-02-01",
               "2018-02-02"
           ]
     }
  ]
}

I want to invite people to my 2-day Birthday Party, but I have to pick a date that best works for the individuals (indexed by their HS). The goal is create a program that can index the Dictionary and return which people can attend based on the dates their availability (2 consecutive days). Right now I created a list detailing the information of each person, but am not sure how to combine them by their available dates. I have:

for index in range(lengthofDic):
     highSchool.append(data["Attendee"][index]["highSchool"])
     email.append(data["Attendee"][index]["email"])
     availDate.append(data["Attendee"][index]["availableDates"])

Which gives the different list containing the information for each person, but I dont know how to combine and filter the list in order to find the best 2 dates that work for people at the same school.

Also tried a function below to filter out the same dates between different values in the list, but dont know how to do it for more than 2 list:

 for index in range(lengthofDic):
      print (lamda x,y: x in (availableDate[1]), (availableDate[1]))  #availableDate is list containing dates

An example of the output that I would want is below:

{
    "Attendee": [
    {
        "aCount": 3,
        "attendees": [
            "BSaggot@***il.com"                
            "JFarough@***il.com"
            "WKlein@***il.com
        ],
        "highSchool": "NorthwestHS",
        "startDate": "2018-02-01"
    },
    {
        "aCount": 0,
        "attendees": [],
        "highSchool": "SouthWestHS,
        "startDate": null
    }

Southwest High School could not attend because the attendee(s) from that school did not have any consecutive 2 day availability so it returned NULL. Any advice on how to structure the program or what methods to use would be greatly appreciated!!

Ajax1234

You can use itertools.groupby:

import itertools
data = {'Invitees': [{'availableDates': ['2018-02-01', '2018-02-02'], 'lastName': 'Klein', 'highSchool': 'NorthwestHS', 'email': 'WKlein@***il.com', 'firstName': 'Will'}, {'availableDates': ['2018-02-04', '2018-01-01'], 'lastName': 'Smith', 'highSchool': 'SouthwestHS', 'email': 'JSmith@***il.com', 'firstName': 'Jack'}, {'availableDates': ['2018-02-01', '2018-02-02'], 'lastName': 'Farough', 'highSchool': 'NorthwestHS', 'email': 'JFarough@***il.com', 'firstName': 'Jamie'}, {'availableDates': ['2018-02-01', '2018-02-02'], 'lastName': 'Saggot', 'highSchool': 'NorthwestHS', 'email': 'BSaggot@***il.com', 'firstName': 'Bob'}]}
new_data = [(a, list(b)) for a, b in itertools.groupby(sorted(data['Invitees'], key=lambda x:int(x['availableDates'][-1].split('-')[-1])-int(x['availableDates'][0].split('-')[-1]), reverse=True), key=lambda x:int(x['availableDates'][-1].split('-')[-1])-int(x['availableDates'][0].split('-')[-1]))]
final_students = {'Attendee':[{"aCount":len(b), "attendees":[c['email'] for c in b], "highschool":[c['highSchool'] for c in b][0], "startDate":[c['availableDates'][0] for c in b][0]} if a == 1 else {'aCount':0, 'attendees':[], 'highschool':[i['highSchool'] for i in b][0], 'startdate':None} for a, b in new_data]}

Output:

{'Attendee': [{'aCount': 3, 'startDate': '2018-02-01', 'highschool': 'NorthwestHS', 'attendees': ['WKlein@***il.com', 'JFarough@***il.com', 'BSaggot@***il.com']}, {'aCount': 0, 'startdate': None, 'highschool': 'SouthwestHS', 'attendees': []}]}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python - Looping through nested dictionaries and fetching values

Update values of a list of dictionaries in python

Convert list of identical dictionaries to Dataframe

Python: Looping over and combining values in nested dictionaries

finding duplicates in a list of dictionaries

Python sum values within nested dictionaries

Compare keys and values of nested dictionaries in Python

Remove duplicate values from list of nested dictionaries

Python - regroup list of dictionaries into two nested list of dictionaries?

finding differences of two list of dictionaries in python

Count the values in list of dictionaries python

Finding the sum of non-zero individual values in nested dictionaries

Python - how to get all the values of nested dictionaries as a single List[dict]?

Python combine values of identical dictionaries without using looping

PYTHON: Finding the average of values of a nested list

Unpacking and testing nested dictionaries values python

Fetching values from dictionaries nested in a list?

Python - Nested Dictionaries - Extracting Values

Sort a list of nested dictionaries by values without knowing its keys [Python]

Getting values from a nested list of dictionaries using python

Comparing values from nested dictionaries in Python 3

Dynamic values in list of dictionaries python

Concatenate values of dictionary of list with nested dictionaries

How to find the values of a key in a nested list of dictionaries in Python

How to get values from list of nested dictionaries?

extract values from nested dictionaries in a list of tuples

Python how to parse a list[dict] in python and convert values from nested dictionaries as keys

Combine multiple identical nested dictionaries of a list by merging the value

List comprehension for dictionaries nested in a list - python