I am writing a python program on the topic of social networks; more specifically X and Facebook. From 3 files, I constructed a nested dictionary with a ‘Names’ sub-dict which has an index number (0-199) as a key, and a name as a value. I then have ‘X Friends’ and ‘FB Friends’ as sub-dicts, which have index numbers as keys as well, and then a list of that person’s friends in the said network for the value. I am trying to write a function called triangle_friendships_x, which takes my nested_dict as a parameter and returns a count of the number of triangle friendships in the X network of my dictionary (A is friends with B, B is friends with C, and C is friends with A). I have been trying to write this function for hours and am very stuck. Could someone please show me how I could achieve this?

Please provide example data.

For each person, you have their friends (that’s 2 people, person → friend).

For each friend, collect *their* friends (that’s 3 people, person → friend → friend).

For each friend of a friend, do they have the original person as a friend?

You then just need to remove duplicates, e.g. A → B → C → A and B → C → A → B.

Are the friendships reciprocal, i.e. if you have A → B, does that also always mean B → A?

If the friendships are always reciprocal, you can sort the members, and if they’re not, you can convert them to a standardised order, e.g. convert B → C → A and C → A → B to A → B → C. This will make it easier to identify duplicates. Making a set of tuples is a simple way.

My apologies. Here is a much shortened preview of my nested dictionary (there are index values 0-199 for each sub-dict):

{‘Names’: {0: ‘Gregory Williams’, 1: ‘George Stewart’, 2: ‘Mark Mercado’, 3: ‘Devin Baldwin’, 4: ‘Samantha Murray’, 5: ‘Brandon Garcia’}, ‘X Friends’ : {0: [‘Chris Ingram’, ‘Brandy Vargas’], 1: [‘Ricky Rangel’, ‘Kenneth Miller DDS’, ‘Tara Smith’, ‘Mark Osborne’], 2: [‘Deborah Simmons’], 3: [‘Sarah White’, ‘Mario Garcia’, ‘Roger Berger’, ‘Brian Bell’, ‘Jenna Griffith’]}, ‘FB Friends’: {0: [‘John Moss’], 1: [‘Katherine Olson’], 2: , 3: [‘Deborah Simmons’, ‘Eric Robinson’, ‘Tammie Gallegos’], 4: [‘Erika Salazar’, ‘John Moss’, ‘Tiffany Avery’, ‘Patricia Henson’]}}