How to list duplicate values in a dictionary that represents flights?












1















I have a dictionary that had another dictionary and some lists inside that.



flightsD={"Delta":{1102:[["IND",1850],["MDW",1955]],
1096:[["PHX",900],["MDW",1255]],
1445:[["ATL",1135],["LAX",1810]],
1776:[["PHL",1350],["RAP",1610]],
1226:[["PHX",950],["MDW",1345]],
1885:[["ATL",1305],["LAX",2000]],
1009:[["MDW",1850],["IND",1955]],
9001:[["MDW",2145],["IND",2255]]},
"Southwestern":{1111:[["SAT",430],["MDW",825]],
2121:[["MDW",430],["SAT",825]],
4335:[["PHX",450],["MDW",745]],
1102:[["MDW",1100],["PHX",1450]]},
"American":{7765:[["IND",1850],["CHA",2105]],
2133:[["BNA",900],["IND",1115]],
3321:[["HOU",1335],["ATL",1615]],
2100:[["BNA",900],["IND",1115]],
4311:[["HOU",905],["ATL",1255]],
5577:[["ATL",1100],["HOU",1350]],
1102:[["BNA",1100],["HOU",1450]]} }


As you can see, Delta, Southwestern and American all have a flights 1102. I would like to list all duplicate flights as a list inside a dictionary.
Expected output:



{1102: ['American', 'Southwestern', 'Delta']}


I have tried to access these values by iterating through the dictionary like so:



for airline in flightsD:
for flights in flightsD[airline]:


I am thinking about making a list to store all of these values but I'm not entirely sure how to go about it










share|improve this question



























    1















    I have a dictionary that had another dictionary and some lists inside that.



    flightsD={"Delta":{1102:[["IND",1850],["MDW",1955]],
    1096:[["PHX",900],["MDW",1255]],
    1445:[["ATL",1135],["LAX",1810]],
    1776:[["PHL",1350],["RAP",1610]],
    1226:[["PHX",950],["MDW",1345]],
    1885:[["ATL",1305],["LAX",2000]],
    1009:[["MDW",1850],["IND",1955]],
    9001:[["MDW",2145],["IND",2255]]},
    "Southwestern":{1111:[["SAT",430],["MDW",825]],
    2121:[["MDW",430],["SAT",825]],
    4335:[["PHX",450],["MDW",745]],
    1102:[["MDW",1100],["PHX",1450]]},
    "American":{7765:[["IND",1850],["CHA",2105]],
    2133:[["BNA",900],["IND",1115]],
    3321:[["HOU",1335],["ATL",1615]],
    2100:[["BNA",900],["IND",1115]],
    4311:[["HOU",905],["ATL",1255]],
    5577:[["ATL",1100],["HOU",1350]],
    1102:[["BNA",1100],["HOU",1450]]} }


    As you can see, Delta, Southwestern and American all have a flights 1102. I would like to list all duplicate flights as a list inside a dictionary.
    Expected output:



    {1102: ['American', 'Southwestern', 'Delta']}


    I have tried to access these values by iterating through the dictionary like so:



    for airline in flightsD:
    for flights in flightsD[airline]:


    I am thinking about making a list to store all of these values but I'm not entirely sure how to go about it










    share|improve this question

























      1












      1








      1








      I have a dictionary that had another dictionary and some lists inside that.



      flightsD={"Delta":{1102:[["IND",1850],["MDW",1955]],
      1096:[["PHX",900],["MDW",1255]],
      1445:[["ATL",1135],["LAX",1810]],
      1776:[["PHL",1350],["RAP",1610]],
      1226:[["PHX",950],["MDW",1345]],
      1885:[["ATL",1305],["LAX",2000]],
      1009:[["MDW",1850],["IND",1955]],
      9001:[["MDW",2145],["IND",2255]]},
      "Southwestern":{1111:[["SAT",430],["MDW",825]],
      2121:[["MDW",430],["SAT",825]],
      4335:[["PHX",450],["MDW",745]],
      1102:[["MDW",1100],["PHX",1450]]},
      "American":{7765:[["IND",1850],["CHA",2105]],
      2133:[["BNA",900],["IND",1115]],
      3321:[["HOU",1335],["ATL",1615]],
      2100:[["BNA",900],["IND",1115]],
      4311:[["HOU",905],["ATL",1255]],
      5577:[["ATL",1100],["HOU",1350]],
      1102:[["BNA",1100],["HOU",1450]]} }


      As you can see, Delta, Southwestern and American all have a flights 1102. I would like to list all duplicate flights as a list inside a dictionary.
      Expected output:



      {1102: ['American', 'Southwestern', 'Delta']}


      I have tried to access these values by iterating through the dictionary like so:



      for airline in flightsD:
      for flights in flightsD[airline]:


      I am thinking about making a list to store all of these values but I'm not entirely sure how to go about it










      share|improve this question














      I have a dictionary that had another dictionary and some lists inside that.



      flightsD={"Delta":{1102:[["IND",1850],["MDW",1955]],
      1096:[["PHX",900],["MDW",1255]],
      1445:[["ATL",1135],["LAX",1810]],
      1776:[["PHL",1350],["RAP",1610]],
      1226:[["PHX",950],["MDW",1345]],
      1885:[["ATL",1305],["LAX",2000]],
      1009:[["MDW",1850],["IND",1955]],
      9001:[["MDW",2145],["IND",2255]]},
      "Southwestern":{1111:[["SAT",430],["MDW",825]],
      2121:[["MDW",430],["SAT",825]],
      4335:[["PHX",450],["MDW",745]],
      1102:[["MDW",1100],["PHX",1450]]},
      "American":{7765:[["IND",1850],["CHA",2105]],
      2133:[["BNA",900],["IND",1115]],
      3321:[["HOU",1335],["ATL",1615]],
      2100:[["BNA",900],["IND",1115]],
      4311:[["HOU",905],["ATL",1255]],
      5577:[["ATL",1100],["HOU",1350]],
      1102:[["BNA",1100],["HOU",1450]]} }


      As you can see, Delta, Southwestern and American all have a flights 1102. I would like to list all duplicate flights as a list inside a dictionary.
      Expected output:



      {1102: ['American', 'Southwestern', 'Delta']}


      I have tried to access these values by iterating through the dictionary like so:



      for airline in flightsD:
      for flights in flightsD[airline]:


      I am thinking about making a list to store all of these values but I'm not entirely sure how to go about it







      python list dictionary






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 21:22









      Caleb CollierCaleb Collier

      83




      83
























          3 Answers
          3






          active

          oldest

          votes


















          4














          You can iterate over the dict items, use dict.setdefault to initialize the output dict with a list to append airlines to, and use a dict comprehension to output items with sublists with more than 1 item:



          d = {}
          for airline, flights in flightsD.items():
          for flight in flights:
          d.setdefault(flight, ).append(airline)
          print({k: v for k, v in d.items() if len(v) > 1})


          This outputs:



          {1102: ['Delta', 'Southwestern', 'American']}





          share|improve this answer

































            1














            You can use defaultdict as follows:



            from collections import defaultdict

            airlines, schedules = flightsD.keys(), flightsD.values()

            flight_nums = [s.keys() for s in schedules]

            duplicates = defaultdict(list)
            for i, item1 in enumerate(flight_nums):
            for j, item2 in enumerate(flight_nums):
            for k in item1:
            if i != j and k in item2 and airlines[i] not in duplicates[k]:
            duplicates[k].append(airlines[i])


            print(duplicates)

            >>> {1102: ['American', 'Southwestern', 'Delta']}





            share|improve this answer































              0














              If you want a dictionary with just those values, you can use the code below (based on the code above):



              list = 
              for flights in flightsD.values():
              list.extend(flights.keys())

              d = {}
              for airline, flights in flightsD.items():
              for flight in flights:
              if list.count(flight) > 1:
              d.setdefault(flight, ).append(airline)





              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345582%2fhow-to-list-duplicate-values-in-a-dictionary-that-represents-flights%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                4














                You can iterate over the dict items, use dict.setdefault to initialize the output dict with a list to append airlines to, and use a dict comprehension to output items with sublists with more than 1 item:



                d = {}
                for airline, flights in flightsD.items():
                for flight in flights:
                d.setdefault(flight, ).append(airline)
                print({k: v for k, v in d.items() if len(v) > 1})


                This outputs:



                {1102: ['Delta', 'Southwestern', 'American']}





                share|improve this answer






























                  4














                  You can iterate over the dict items, use dict.setdefault to initialize the output dict with a list to append airlines to, and use a dict comprehension to output items with sublists with more than 1 item:



                  d = {}
                  for airline, flights in flightsD.items():
                  for flight in flights:
                  d.setdefault(flight, ).append(airline)
                  print({k: v for k, v in d.items() if len(v) > 1})


                  This outputs:



                  {1102: ['Delta', 'Southwestern', 'American']}





                  share|improve this answer




























                    4












                    4








                    4







                    You can iterate over the dict items, use dict.setdefault to initialize the output dict with a list to append airlines to, and use a dict comprehension to output items with sublists with more than 1 item:



                    d = {}
                    for airline, flights in flightsD.items():
                    for flight in flights:
                    d.setdefault(flight, ).append(airline)
                    print({k: v for k, v in d.items() if len(v) > 1})


                    This outputs:



                    {1102: ['Delta', 'Southwestern', 'American']}





                    share|improve this answer















                    You can iterate over the dict items, use dict.setdefault to initialize the output dict with a list to append airlines to, and use a dict comprehension to output items with sublists with more than 1 item:



                    d = {}
                    for airline, flights in flightsD.items():
                    for flight in flights:
                    d.setdefault(flight, ).append(airline)
                    print({k: v for k, v in d.items() if len(v) > 1})


                    This outputs:



                    {1102: ['Delta', 'Southwestern', 'American']}






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 16 '18 at 21:38

























                    answered Nov 16 '18 at 21:30









                    blhsingblhsing

                    30.5k41336




                    30.5k41336

























                        1














                        You can use defaultdict as follows:



                        from collections import defaultdict

                        airlines, schedules = flightsD.keys(), flightsD.values()

                        flight_nums = [s.keys() for s in schedules]

                        duplicates = defaultdict(list)
                        for i, item1 in enumerate(flight_nums):
                        for j, item2 in enumerate(flight_nums):
                        for k in item1:
                        if i != j and k in item2 and airlines[i] not in duplicates[k]:
                        duplicates[k].append(airlines[i])


                        print(duplicates)

                        >>> {1102: ['American', 'Southwestern', 'Delta']}





                        share|improve this answer




























                          1














                          You can use defaultdict as follows:



                          from collections import defaultdict

                          airlines, schedules = flightsD.keys(), flightsD.values()

                          flight_nums = [s.keys() for s in schedules]

                          duplicates = defaultdict(list)
                          for i, item1 in enumerate(flight_nums):
                          for j, item2 in enumerate(flight_nums):
                          for k in item1:
                          if i != j and k in item2 and airlines[i] not in duplicates[k]:
                          duplicates[k].append(airlines[i])


                          print(duplicates)

                          >>> {1102: ['American', 'Southwestern', 'Delta']}





                          share|improve this answer


























                            1












                            1








                            1







                            You can use defaultdict as follows:



                            from collections import defaultdict

                            airlines, schedules = flightsD.keys(), flightsD.values()

                            flight_nums = [s.keys() for s in schedules]

                            duplicates = defaultdict(list)
                            for i, item1 in enumerate(flight_nums):
                            for j, item2 in enumerate(flight_nums):
                            for k in item1:
                            if i != j and k in item2 and airlines[i] not in duplicates[k]:
                            duplicates[k].append(airlines[i])


                            print(duplicates)

                            >>> {1102: ['American', 'Southwestern', 'Delta']}





                            share|improve this answer













                            You can use defaultdict as follows:



                            from collections import defaultdict

                            airlines, schedules = flightsD.keys(), flightsD.values()

                            flight_nums = [s.keys() for s in schedules]

                            duplicates = defaultdict(list)
                            for i, item1 in enumerate(flight_nums):
                            for j, item2 in enumerate(flight_nums):
                            for k in item1:
                            if i != j and k in item2 and airlines[i] not in duplicates[k]:
                            duplicates[k].append(airlines[i])


                            print(duplicates)

                            >>> {1102: ['American', 'Southwestern', 'Delta']}






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 16 '18 at 21:52









                            berkelemberkelem

                            8682720




                            8682720























                                0














                                If you want a dictionary with just those values, you can use the code below (based on the code above):



                                list = 
                                for flights in flightsD.values():
                                list.extend(flights.keys())

                                d = {}
                                for airline, flights in flightsD.items():
                                for flight in flights:
                                if list.count(flight) > 1:
                                d.setdefault(flight, ).append(airline)





                                share|improve this answer




























                                  0














                                  If you want a dictionary with just those values, you can use the code below (based on the code above):



                                  list = 
                                  for flights in flightsD.values():
                                  list.extend(flights.keys())

                                  d = {}
                                  for airline, flights in flightsD.items():
                                  for flight in flights:
                                  if list.count(flight) > 1:
                                  d.setdefault(flight, ).append(airline)





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    If you want a dictionary with just those values, you can use the code below (based on the code above):



                                    list = 
                                    for flights in flightsD.values():
                                    list.extend(flights.keys())

                                    d = {}
                                    for airline, flights in flightsD.items():
                                    for flight in flights:
                                    if list.count(flight) > 1:
                                    d.setdefault(flight, ).append(airline)





                                    share|improve this answer













                                    If you want a dictionary with just those values, you can use the code below (based on the code above):



                                    list = 
                                    for flights in flightsD.values():
                                    list.extend(flights.keys())

                                    d = {}
                                    for airline, flights in flightsD.items():
                                    for flight in flights:
                                    if list.count(flight) > 1:
                                    d.setdefault(flight, ).append(airline)






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 16 '18 at 21:50









                                    Ellie HannaEllie Hanna

                                    585




                                    585






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345582%2fhow-to-list-duplicate-values-in-a-dictionary-that-represents-flights%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        這個網誌中的熱門文章

                                        Academy of Television Arts & Sciences

                                        L'Équipe

                                        1995 France bombings