how to create a list that will store many values from a list of dictionaries












-2















I have a list of Dictionaries in which airbnb[0] is



{
'room_id': '1133718',
'survey_id': '1280',
'host_id': '6219420',
'room_type': 'Shared room',
'country': '',
'city': 'Singapore',
'borough': '',
'neighborhood': 'MK03',
'reviews': 9.0,
'overall_satisfaction': 4.5,
'accommodates': '12',
'bedrooms': '1.0',
'bathrooms': '',
'price': 74.0,
'minstay': '',
'last_modified': '2017-05-17 09:10:25.431659',
'latitude': 1.293354,
'longitude': 103.769226,
'location': '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F'
}


how do I go about it if I want to get a list consisting of only the room_id Value and the price for each dictionary in my list of dictionaries so that I can compile those lists in my new_list?










share|improve this question




















  • 2





    You can use a for loop to iterate over all the dictionaries and do the extraction there. Did you try something which didn't work? If yes, please include that in your question too.

    – slider
    Nov 22 '18 at 15:51











  • value is not a field in that dictionary, so you won't be able to store it unless it's a field you can calculate

    – C.Nivs
    Nov 22 '18 at 15:57
















-2















I have a list of Dictionaries in which airbnb[0] is



{
'room_id': '1133718',
'survey_id': '1280',
'host_id': '6219420',
'room_type': 'Shared room',
'country': '',
'city': 'Singapore',
'borough': '',
'neighborhood': 'MK03',
'reviews': 9.0,
'overall_satisfaction': 4.5,
'accommodates': '12',
'bedrooms': '1.0',
'bathrooms': '',
'price': 74.0,
'minstay': '',
'last_modified': '2017-05-17 09:10:25.431659',
'latitude': 1.293354,
'longitude': 103.769226,
'location': '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F'
}


how do I go about it if I want to get a list consisting of only the room_id Value and the price for each dictionary in my list of dictionaries so that I can compile those lists in my new_list?










share|improve this question




















  • 2





    You can use a for loop to iterate over all the dictionaries and do the extraction there. Did you try something which didn't work? If yes, please include that in your question too.

    – slider
    Nov 22 '18 at 15:51











  • value is not a field in that dictionary, so you won't be able to store it unless it's a field you can calculate

    – C.Nivs
    Nov 22 '18 at 15:57














-2












-2








-2








I have a list of Dictionaries in which airbnb[0] is



{
'room_id': '1133718',
'survey_id': '1280',
'host_id': '6219420',
'room_type': 'Shared room',
'country': '',
'city': 'Singapore',
'borough': '',
'neighborhood': 'MK03',
'reviews': 9.0,
'overall_satisfaction': 4.5,
'accommodates': '12',
'bedrooms': '1.0',
'bathrooms': '',
'price': 74.0,
'minstay': '',
'last_modified': '2017-05-17 09:10:25.431659',
'latitude': 1.293354,
'longitude': 103.769226,
'location': '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F'
}


how do I go about it if I want to get a list consisting of only the room_id Value and the price for each dictionary in my list of dictionaries so that I can compile those lists in my new_list?










share|improve this question
















I have a list of Dictionaries in which airbnb[0] is



{
'room_id': '1133718',
'survey_id': '1280',
'host_id': '6219420',
'room_type': 'Shared room',
'country': '',
'city': 'Singapore',
'borough': '',
'neighborhood': 'MK03',
'reviews': 9.0,
'overall_satisfaction': 4.5,
'accommodates': '12',
'bedrooms': '1.0',
'bathrooms': '',
'price': 74.0,
'minstay': '',
'last_modified': '2017-05-17 09:10:25.431659',
'latitude': 1.293354,
'longitude': 103.769226,
'location': '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F'
}


how do I go about it if I want to get a list consisting of only the room_id Value and the price for each dictionary in my list of dictionaries so that I can compile those lists in my new_list?







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 15:49









slider

8,25811130




8,25811130










asked Nov 22 '18 at 15:48









Bryan97Bryan97

1




1








  • 2





    You can use a for loop to iterate over all the dictionaries and do the extraction there. Did you try something which didn't work? If yes, please include that in your question too.

    – slider
    Nov 22 '18 at 15:51











  • value is not a field in that dictionary, so you won't be able to store it unless it's a field you can calculate

    – C.Nivs
    Nov 22 '18 at 15:57














  • 2





    You can use a for loop to iterate over all the dictionaries and do the extraction there. Did you try something which didn't work? If yes, please include that in your question too.

    – slider
    Nov 22 '18 at 15:51











  • value is not a field in that dictionary, so you won't be able to store it unless it's a field you can calculate

    – C.Nivs
    Nov 22 '18 at 15:57








2




2





You can use a for loop to iterate over all the dictionaries and do the extraction there. Did you try something which didn't work? If yes, please include that in your question too.

– slider
Nov 22 '18 at 15:51





You can use a for loop to iterate over all the dictionaries and do the extraction there. Did you try something which didn't work? If yes, please include that in your question too.

– slider
Nov 22 '18 at 15:51













value is not a field in that dictionary, so you won't be able to store it unless it's a field you can calculate

– C.Nivs
Nov 22 '18 at 15:57





value is not a field in that dictionary, so you won't be able to store it unless it's a field you can calculate

– C.Nivs
Nov 22 '18 at 15:57












4 Answers
4






active

oldest

votes


















1














Not sure if this is what you're after but you can make a dictionary where the key is the room_id and the value the price for each property like so:



room_prices = { room['room_id'] : room['price'] for room in airbnb }


Then you access the price for a given room like so:



room_id = '1133718'
room_price = room_prices[room_id]





share|improve this answer































    0














    If you want them as tuples:



    new_list = [(x['room_id'], x['price']) for x in airbnb]
    # returns
    [('1133718', 74.0)]


    or a dict:



    new_list = [{'room_id': x['room_id'], 'price': x['price']} for x in airbnb]
    # returns
    [{'room_id': '1133718', 'price': 74.0}]





    share|improve this answer































      0














      A list comprehension selecting target keys in your list of dictionaries should do the job, assuming your list contains multiple dictionaries.



      room_info =[{
      'room_id': '1133718',
      'survey_id': '1280',
      'host_id': '6219420',
      'room_type': 'Shared room',
      'country': '',
      'city': 'Singapore',
      'borough': '',
      'neighborhood': 'MK03',
      'reviews': 9.0,
      'overall_satisfaction': 4.5,
      'accommodates': '12',
      'bedrooms': '1.0',
      'bathrooms': '',
      'price': 74.0,
      'minstay': '',
      'last_modified': '2017-05-17 09:10:25.431659',
      'latitude': 1.293354,
      'longitude': 103.769226,
      'location': '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F'
      },
      {
      'room_id': '1133718',
      'survey_id': '1280',
      'host_id': '6219420',
      'room_type': 'Shared room',
      'country': '',
      'city': 'Singapore',
      'borough': '',
      'neighborhood': 'MK03',
      'reviews': 9.0,
      'overall_satisfaction': 4.5,
      'accommodates': '12',
      'bedrooms': '1.0',
      'bathrooms': '',
      'price': 74.0,
      'minstay': '',
      'last_modified': '2017-05-17 09:10:25.431659',
      'latitude': 1.293354,
      'longitude': 103.769226,
      'location': '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F'
      }]

      [[i['room_id'],i['price']] for i in room_info]
      >>[['1133718', 74.0], ['1133718', 74.0]]


      The result will return a nested list where each individual list contains the room_id and price detail.






      share|improve this answer































        0














        It's easy to extract one element of the dict into a new list:



        room_ids = [item.get('room_id') for item in airbnb]


        Do that for all interesting ones and generate a new list of dicts, if you don't want separate lists. Or you can do all that in one loop



        newlist = [{'room_id': item.get('room_id'), 'price': item.get('price')} for item in airbnb]


        EDIT: Or a bit more verbose but more general:



        mylist = [{'a': 1, 'b':2, 'c':1}, {'a': 2, 'b': 2, 'c':1}, {'a': 5, 'b': 2, 'c':1}, {'b': 5}]
        interesting_keys = ['a', 'b']
        newlist =
        for item in mylist:
        d = dict()
        for i in interesting_keys:
        d[i] = item.get(i)
        newlist.append(d)
        print(nl)


        will output:



        [{'a': 1, 'b': 2}, {'a': 2, 'b': 2}, {'a': 5, 'b': 2}, {'a': None, 'b': 5}]





        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%2f53434446%2fhow-to-create-a-list-that-will-store-many-values-from-a-list-of-dictionaries%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Not sure if this is what you're after but you can make a dictionary where the key is the room_id and the value the price for each property like so:



          room_prices = { room['room_id'] : room['price'] for room in airbnb }


          Then you access the price for a given room like so:



          room_id = '1133718'
          room_price = room_prices[room_id]





          share|improve this answer




























            1














            Not sure if this is what you're after but you can make a dictionary where the key is the room_id and the value the price for each property like so:



            room_prices = { room['room_id'] : room['price'] for room in airbnb }


            Then you access the price for a given room like so:



            room_id = '1133718'
            room_price = room_prices[room_id]





            share|improve this answer


























              1












              1








              1







              Not sure if this is what you're after but you can make a dictionary where the key is the room_id and the value the price for each property like so:



              room_prices = { room['room_id'] : room['price'] for room in airbnb }


              Then you access the price for a given room like so:



              room_id = '1133718'
              room_price = room_prices[room_id]





              share|improve this answer













              Not sure if this is what you're after but you can make a dictionary where the key is the room_id and the value the price for each property like so:



              room_prices = { room['room_id'] : room['price'] for room in airbnb }


              Then you access the price for a given room like so:



              room_id = '1133718'
              room_price = room_prices[room_id]






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 22 '18 at 16:04









              T BurgisT Burgis

              91017




              91017

























                  0














                  If you want them as tuples:



                  new_list = [(x['room_id'], x['price']) for x in airbnb]
                  # returns
                  [('1133718', 74.0)]


                  or a dict:



                  new_list = [{'room_id': x['room_id'], 'price': x['price']} for x in airbnb]
                  # returns
                  [{'room_id': '1133718', 'price': 74.0}]





                  share|improve this answer




























                    0














                    If you want them as tuples:



                    new_list = [(x['room_id'], x['price']) for x in airbnb]
                    # returns
                    [('1133718', 74.0)]


                    or a dict:



                    new_list = [{'room_id': x['room_id'], 'price': x['price']} for x in airbnb]
                    # returns
                    [{'room_id': '1133718', 'price': 74.0}]





                    share|improve this answer


























                      0












                      0








                      0







                      If you want them as tuples:



                      new_list = [(x['room_id'], x['price']) for x in airbnb]
                      # returns
                      [('1133718', 74.0)]


                      or a dict:



                      new_list = [{'room_id': x['room_id'], 'price': x['price']} for x in airbnb]
                      # returns
                      [{'room_id': '1133718', 'price': 74.0}]





                      share|improve this answer













                      If you want them as tuples:



                      new_list = [(x['room_id'], x['price']) for x in airbnb]
                      # returns
                      [('1133718', 74.0)]


                      or a dict:



                      new_list = [{'room_id': x['room_id'], 'price': x['price']} for x in airbnb]
                      # returns
                      [{'room_id': '1133718', 'price': 74.0}]






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 22 '18 at 16:02









                      AlexAlex

                      763621




                      763621























                          0














                          A list comprehension selecting target keys in your list of dictionaries should do the job, assuming your list contains multiple dictionaries.



                          room_info =[{
                          'room_id': '1133718',
                          'survey_id': '1280',
                          'host_id': '6219420',
                          'room_type': 'Shared room',
                          'country': '',
                          'city': 'Singapore',
                          'borough': '',
                          'neighborhood': 'MK03',
                          'reviews': 9.0,
                          'overall_satisfaction': 4.5,
                          'accommodates': '12',
                          'bedrooms': '1.0',
                          'bathrooms': '',
                          'price': 74.0,
                          'minstay': '',
                          'last_modified': '2017-05-17 09:10:25.431659',
                          'latitude': 1.293354,
                          'longitude': 103.769226,
                          'location': '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F'
                          },
                          {
                          'room_id': '1133718',
                          'survey_id': '1280',
                          'host_id': '6219420',
                          'room_type': 'Shared room',
                          'country': '',
                          'city': 'Singapore',
                          'borough': '',
                          'neighborhood': 'MK03',
                          'reviews': 9.0,
                          'overall_satisfaction': 4.5,
                          'accommodates': '12',
                          'bedrooms': '1.0',
                          'bathrooms': '',
                          'price': 74.0,
                          'minstay': '',
                          'last_modified': '2017-05-17 09:10:25.431659',
                          'latitude': 1.293354,
                          'longitude': 103.769226,
                          'location': '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F'
                          }]

                          [[i['room_id'],i['price']] for i in room_info]
                          >>[['1133718', 74.0], ['1133718', 74.0]]


                          The result will return a nested list where each individual list contains the room_id and price detail.






                          share|improve this answer




























                            0














                            A list comprehension selecting target keys in your list of dictionaries should do the job, assuming your list contains multiple dictionaries.



                            room_info =[{
                            'room_id': '1133718',
                            'survey_id': '1280',
                            'host_id': '6219420',
                            'room_type': 'Shared room',
                            'country': '',
                            'city': 'Singapore',
                            'borough': '',
                            'neighborhood': 'MK03',
                            'reviews': 9.0,
                            'overall_satisfaction': 4.5,
                            'accommodates': '12',
                            'bedrooms': '1.0',
                            'bathrooms': '',
                            'price': 74.0,
                            'minstay': '',
                            'last_modified': '2017-05-17 09:10:25.431659',
                            'latitude': 1.293354,
                            'longitude': 103.769226,
                            'location': '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F'
                            },
                            {
                            'room_id': '1133718',
                            'survey_id': '1280',
                            'host_id': '6219420',
                            'room_type': 'Shared room',
                            'country': '',
                            'city': 'Singapore',
                            'borough': '',
                            'neighborhood': 'MK03',
                            'reviews': 9.0,
                            'overall_satisfaction': 4.5,
                            'accommodates': '12',
                            'bedrooms': '1.0',
                            'bathrooms': '',
                            'price': 74.0,
                            'minstay': '',
                            'last_modified': '2017-05-17 09:10:25.431659',
                            'latitude': 1.293354,
                            'longitude': 103.769226,
                            'location': '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F'
                            }]

                            [[i['room_id'],i['price']] for i in room_info]
                            >>[['1133718', 74.0], ['1133718', 74.0]]


                            The result will return a nested list where each individual list contains the room_id and price detail.






                            share|improve this answer


























                              0












                              0








                              0







                              A list comprehension selecting target keys in your list of dictionaries should do the job, assuming your list contains multiple dictionaries.



                              room_info =[{
                              'room_id': '1133718',
                              'survey_id': '1280',
                              'host_id': '6219420',
                              'room_type': 'Shared room',
                              'country': '',
                              'city': 'Singapore',
                              'borough': '',
                              'neighborhood': 'MK03',
                              'reviews': 9.0,
                              'overall_satisfaction': 4.5,
                              'accommodates': '12',
                              'bedrooms': '1.0',
                              'bathrooms': '',
                              'price': 74.0,
                              'minstay': '',
                              'last_modified': '2017-05-17 09:10:25.431659',
                              'latitude': 1.293354,
                              'longitude': 103.769226,
                              'location': '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F'
                              },
                              {
                              'room_id': '1133718',
                              'survey_id': '1280',
                              'host_id': '6219420',
                              'room_type': 'Shared room',
                              'country': '',
                              'city': 'Singapore',
                              'borough': '',
                              'neighborhood': 'MK03',
                              'reviews': 9.0,
                              'overall_satisfaction': 4.5,
                              'accommodates': '12',
                              'bedrooms': '1.0',
                              'bathrooms': '',
                              'price': 74.0,
                              'minstay': '',
                              'last_modified': '2017-05-17 09:10:25.431659',
                              'latitude': 1.293354,
                              'longitude': 103.769226,
                              'location': '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F'
                              }]

                              [[i['room_id'],i['price']] for i in room_info]
                              >>[['1133718', 74.0], ['1133718', 74.0]]


                              The result will return a nested list where each individual list contains the room_id and price detail.






                              share|improve this answer













                              A list comprehension selecting target keys in your list of dictionaries should do the job, assuming your list contains multiple dictionaries.



                              room_info =[{
                              'room_id': '1133718',
                              'survey_id': '1280',
                              'host_id': '6219420',
                              'room_type': 'Shared room',
                              'country': '',
                              'city': 'Singapore',
                              'borough': '',
                              'neighborhood': 'MK03',
                              'reviews': 9.0,
                              'overall_satisfaction': 4.5,
                              'accommodates': '12',
                              'bedrooms': '1.0',
                              'bathrooms': '',
                              'price': 74.0,
                              'minstay': '',
                              'last_modified': '2017-05-17 09:10:25.431659',
                              'latitude': 1.293354,
                              'longitude': 103.769226,
                              'location': '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F'
                              },
                              {
                              'room_id': '1133718',
                              'survey_id': '1280',
                              'host_id': '6219420',
                              'room_type': 'Shared room',
                              'country': '',
                              'city': 'Singapore',
                              'borough': '',
                              'neighborhood': 'MK03',
                              'reviews': 9.0,
                              'overall_satisfaction': 4.5,
                              'accommodates': '12',
                              'bedrooms': '1.0',
                              'bathrooms': '',
                              'price': 74.0,
                              'minstay': '',
                              'last_modified': '2017-05-17 09:10:25.431659',
                              'latitude': 1.293354,
                              'longitude': 103.769226,
                              'location': '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F'
                              }]

                              [[i['room_id'],i['price']] for i in room_info]
                              >>[['1133718', 74.0], ['1133718', 74.0]]


                              The result will return a nested list where each individual list contains the room_id and price detail.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 22 '18 at 16:02









                              BernardLBernardL

                              2,37311029




                              2,37311029























                                  0














                                  It's easy to extract one element of the dict into a new list:



                                  room_ids = [item.get('room_id') for item in airbnb]


                                  Do that for all interesting ones and generate a new list of dicts, if you don't want separate lists. Or you can do all that in one loop



                                  newlist = [{'room_id': item.get('room_id'), 'price': item.get('price')} for item in airbnb]


                                  EDIT: Or a bit more verbose but more general:



                                  mylist = [{'a': 1, 'b':2, 'c':1}, {'a': 2, 'b': 2, 'c':1}, {'a': 5, 'b': 2, 'c':1}, {'b': 5}]
                                  interesting_keys = ['a', 'b']
                                  newlist =
                                  for item in mylist:
                                  d = dict()
                                  for i in interesting_keys:
                                  d[i] = item.get(i)
                                  newlist.append(d)
                                  print(nl)


                                  will output:



                                  [{'a': 1, 'b': 2}, {'a': 2, 'b': 2}, {'a': 5, 'b': 2}, {'a': None, 'b': 5}]





                                  share|improve this answer






























                                    0














                                    It's easy to extract one element of the dict into a new list:



                                    room_ids = [item.get('room_id') for item in airbnb]


                                    Do that for all interesting ones and generate a new list of dicts, if you don't want separate lists. Or you can do all that in one loop



                                    newlist = [{'room_id': item.get('room_id'), 'price': item.get('price')} for item in airbnb]


                                    EDIT: Or a bit more verbose but more general:



                                    mylist = [{'a': 1, 'b':2, 'c':1}, {'a': 2, 'b': 2, 'c':1}, {'a': 5, 'b': 2, 'c':1}, {'b': 5}]
                                    interesting_keys = ['a', 'b']
                                    newlist =
                                    for item in mylist:
                                    d = dict()
                                    for i in interesting_keys:
                                    d[i] = item.get(i)
                                    newlist.append(d)
                                    print(nl)


                                    will output:



                                    [{'a': 1, 'b': 2}, {'a': 2, 'b': 2}, {'a': 5, 'b': 2}, {'a': None, 'b': 5}]





                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      It's easy to extract one element of the dict into a new list:



                                      room_ids = [item.get('room_id') for item in airbnb]


                                      Do that for all interesting ones and generate a new list of dicts, if you don't want separate lists. Or you can do all that in one loop



                                      newlist = [{'room_id': item.get('room_id'), 'price': item.get('price')} for item in airbnb]


                                      EDIT: Or a bit more verbose but more general:



                                      mylist = [{'a': 1, 'b':2, 'c':1}, {'a': 2, 'b': 2, 'c':1}, {'a': 5, 'b': 2, 'c':1}, {'b': 5}]
                                      interesting_keys = ['a', 'b']
                                      newlist =
                                      for item in mylist:
                                      d = dict()
                                      for i in interesting_keys:
                                      d[i] = item.get(i)
                                      newlist.append(d)
                                      print(nl)


                                      will output:



                                      [{'a': 1, 'b': 2}, {'a': 2, 'b': 2}, {'a': 5, 'b': 2}, {'a': None, 'b': 5}]





                                      share|improve this answer















                                      It's easy to extract one element of the dict into a new list:



                                      room_ids = [item.get('room_id') for item in airbnb]


                                      Do that for all interesting ones and generate a new list of dicts, if you don't want separate lists. Or you can do all that in one loop



                                      newlist = [{'room_id': item.get('room_id'), 'price': item.get('price')} for item in airbnb]


                                      EDIT: Or a bit more verbose but more general:



                                      mylist = [{'a': 1, 'b':2, 'c':1}, {'a': 2, 'b': 2, 'c':1}, {'a': 5, 'b': 2, 'c':1}, {'b': 5}]
                                      interesting_keys = ['a', 'b']
                                      newlist =
                                      for item in mylist:
                                      d = dict()
                                      for i in interesting_keys:
                                      d[i] = item.get(i)
                                      newlist.append(d)
                                      print(nl)


                                      will output:



                                      [{'a': 1, 'b': 2}, {'a': 2, 'b': 2}, {'a': 5, 'b': 2}, {'a': None, 'b': 5}]






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 22 '18 at 16:13

























                                      answered Nov 22 '18 at 16:01









                                      planetmakerplanetmaker

                                      4,63421629




                                      4,63421629






























                                          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%2f53434446%2fhow-to-create-a-list-that-will-store-many-values-from-a-list-of-dictionaries%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







                                          Popular posts from this blog

                                          Ottavio Pratesi

                                          Tricia Helfer

                                          15 giugno