Getting “keys” from list of dicts [duplicate]












1















This question already has an answer here:




  • How to return dictionary keys as a list in Python?

    10 answers




I have the following data:



[{'id': ['132605', '132750', '132772', '132773', '133065', '133150', '133185', '133188', '133271', '133298']}, 
{'number': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']},
{'id': ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1']}]


What would be the best way to get a list of the keys (as if it was a dict and not an array)? Currently I'm doing:



>>> [list(i.keys())[0] for i in e.original_column_data]
['id', 'number', 'id']


But that feels a bit hackish










share|improve this question













marked as duplicate by jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 27 '18 at 22:44


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    1















    This question already has an answer here:




    • How to return dictionary keys as a list in Python?

      10 answers




    I have the following data:



    [{'id': ['132605', '132750', '132772', '132773', '133065', '133150', '133185', '133188', '133271', '133298']}, 
    {'number': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']},
    {'id': ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1']}]


    What would be the best way to get a list of the keys (as if it was a dict and not an array)? Currently I'm doing:



    >>> [list(i.keys())[0] for i in e.original_column_data]
    ['id', 'number', 'id']


    But that feels a bit hackish










    share|improve this question













    marked as duplicate by jpp python
    Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Dec 27 '18 at 22:44


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      1












      1








      1








      This question already has an answer here:




      • How to return dictionary keys as a list in Python?

        10 answers




      I have the following data:



      [{'id': ['132605', '132750', '132772', '132773', '133065', '133150', '133185', '133188', '133271', '133298']}, 
      {'number': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']},
      {'id': ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1']}]


      What would be the best way to get a list of the keys (as if it was a dict and not an array)? Currently I'm doing:



      >>> [list(i.keys())[0] for i in e.original_column_data]
      ['id', 'number', 'id']


      But that feels a bit hackish










      share|improve this question














      This question already has an answer here:




      • How to return dictionary keys as a list in Python?

        10 answers




      I have the following data:



      [{'id': ['132605', '132750', '132772', '132773', '133065', '133150', '133185', '133188', '133271', '133298']}, 
      {'number': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']},
      {'id': ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1']}]


      What would be the best way to get a list of the keys (as if it was a dict and not an array)? Currently I'm doing:



      >>> [list(i.keys())[0] for i in e.original_column_data]
      ['id', 'number', 'id']


      But that feels a bit hackish





      This question already has an answer here:




      • How to return dictionary keys as a list in Python?

        10 answers








      python






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 27 '18 at 20:14









      David L

      30213




      30213




      marked as duplicate by jpp python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Dec 27 '18 at 22:44


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by jpp python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Dec 27 '18 at 22:44


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          3 Answers
          3






          active

          oldest

          votes


















          5














          What is hacky about it? It's a bit inelegant. You just need to do the following:



          >>> keys = 
          >>> data = [{'id': ['132605', '132750', '132772', '132773', '133065', '133150', '133185', '133188', '133271', '133298']},
          ... {'number': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']},
          ... {'id': ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1']}]
          >>> for d in data:
          ... keys.extend(d)
          ...
          >>> keys
          ['id', 'number', 'id']


          Or if you prefer one-liners:



          >>> [k for d in data for k in d]
          ['id', 'number', 'id']





          share|improve this answer





















          • thanks for these answers, these are very helpful. Out of curiosity, why does keys.extend(d) only add the key and not the entire dict there? I actually had to test it to make sure it worked because it seemed so counterintuitive. (Unlike append)
            – David L
            Dec 27 '18 at 20:20








          • 1




            .extend takes an iterable, and adds all the elements in the iterable to the list. dict objects are iterables over their keys. If you want an iterable of the values, use d.values(), if you want an iterable of the key-value pairs, use d.items()
            – juanpa.arrivillaga
            Dec 27 '18 at 20:21










          • @DavidL well obviously because .extend and .append are different!
            – juanpa.arrivillaga
            Dec 27 '18 at 20:22



















          2














          This is simpler and does the same thing:



          [k for d in e.original_column_data for k in d]
          => ['id', 'number', 'id']





          share|improve this answer































            2














            first way



            iteration on a dictionary gives u its keys, so a simple



            >>> [key for key in dict]


            gives u a list of keys and you can get what you want with



            >>> [key for dict in dict_list for key in dict]


            second way



            use .key() (used in your code)
            but there is no need to use list() (edit: for python 2)

            here's what it will look like:



            >>> [dict.keys()[0] for dict in dict_list]


            in your code, dictionaries have only one key so these two has the same result.

            but I prefer the first one since it gives all keys of all the dictionaries






            share|improve this answer



















            • 1




              dict.keys()[0] will not work in Python 3
              – juanpa.arrivillaga
              Dec 27 '18 at 21:11


















            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            5














            What is hacky about it? It's a bit inelegant. You just need to do the following:



            >>> keys = 
            >>> data = [{'id': ['132605', '132750', '132772', '132773', '133065', '133150', '133185', '133188', '133271', '133298']},
            ... {'number': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']},
            ... {'id': ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1']}]
            >>> for d in data:
            ... keys.extend(d)
            ...
            >>> keys
            ['id', 'number', 'id']


            Or if you prefer one-liners:



            >>> [k for d in data for k in d]
            ['id', 'number', 'id']





            share|improve this answer





















            • thanks for these answers, these are very helpful. Out of curiosity, why does keys.extend(d) only add the key and not the entire dict there? I actually had to test it to make sure it worked because it seemed so counterintuitive. (Unlike append)
              – David L
              Dec 27 '18 at 20:20








            • 1




              .extend takes an iterable, and adds all the elements in the iterable to the list. dict objects are iterables over their keys. If you want an iterable of the values, use d.values(), if you want an iterable of the key-value pairs, use d.items()
              – juanpa.arrivillaga
              Dec 27 '18 at 20:21










            • @DavidL well obviously because .extend and .append are different!
              – juanpa.arrivillaga
              Dec 27 '18 at 20:22
















            5














            What is hacky about it? It's a bit inelegant. You just need to do the following:



            >>> keys = 
            >>> data = [{'id': ['132605', '132750', '132772', '132773', '133065', '133150', '133185', '133188', '133271', '133298']},
            ... {'number': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']},
            ... {'id': ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1']}]
            >>> for d in data:
            ... keys.extend(d)
            ...
            >>> keys
            ['id', 'number', 'id']


            Or if you prefer one-liners:



            >>> [k for d in data for k in d]
            ['id', 'number', 'id']





            share|improve this answer





















            • thanks for these answers, these are very helpful. Out of curiosity, why does keys.extend(d) only add the key and not the entire dict there? I actually had to test it to make sure it worked because it seemed so counterintuitive. (Unlike append)
              – David L
              Dec 27 '18 at 20:20








            • 1




              .extend takes an iterable, and adds all the elements in the iterable to the list. dict objects are iterables over their keys. If you want an iterable of the values, use d.values(), if you want an iterable of the key-value pairs, use d.items()
              – juanpa.arrivillaga
              Dec 27 '18 at 20:21










            • @DavidL well obviously because .extend and .append are different!
              – juanpa.arrivillaga
              Dec 27 '18 at 20:22














            5












            5








            5






            What is hacky about it? It's a bit inelegant. You just need to do the following:



            >>> keys = 
            >>> data = [{'id': ['132605', '132750', '132772', '132773', '133065', '133150', '133185', '133188', '133271', '133298']},
            ... {'number': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']},
            ... {'id': ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1']}]
            >>> for d in data:
            ... keys.extend(d)
            ...
            >>> keys
            ['id', 'number', 'id']


            Or if you prefer one-liners:



            >>> [k for d in data for k in d]
            ['id', 'number', 'id']





            share|improve this answer












            What is hacky about it? It's a bit inelegant. You just need to do the following:



            >>> keys = 
            >>> data = [{'id': ['132605', '132750', '132772', '132773', '133065', '133150', '133185', '133188', '133271', '133298']},
            ... {'number': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']},
            ... {'id': ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1']}]
            >>> for d in data:
            ... keys.extend(d)
            ...
            >>> keys
            ['id', 'number', 'id']


            Or if you prefer one-liners:



            >>> [k for d in data for k in d]
            ['id', 'number', 'id']






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 27 '18 at 20:17









            juanpa.arrivillaga

            37.2k33470




            37.2k33470












            • thanks for these answers, these are very helpful. Out of curiosity, why does keys.extend(d) only add the key and not the entire dict there? I actually had to test it to make sure it worked because it seemed so counterintuitive. (Unlike append)
              – David L
              Dec 27 '18 at 20:20








            • 1




              .extend takes an iterable, and adds all the elements in the iterable to the list. dict objects are iterables over their keys. If you want an iterable of the values, use d.values(), if you want an iterable of the key-value pairs, use d.items()
              – juanpa.arrivillaga
              Dec 27 '18 at 20:21










            • @DavidL well obviously because .extend and .append are different!
              – juanpa.arrivillaga
              Dec 27 '18 at 20:22


















            • thanks for these answers, these are very helpful. Out of curiosity, why does keys.extend(d) only add the key and not the entire dict there? I actually had to test it to make sure it worked because it seemed so counterintuitive. (Unlike append)
              – David L
              Dec 27 '18 at 20:20








            • 1




              .extend takes an iterable, and adds all the elements in the iterable to the list. dict objects are iterables over their keys. If you want an iterable of the values, use d.values(), if you want an iterable of the key-value pairs, use d.items()
              – juanpa.arrivillaga
              Dec 27 '18 at 20:21










            • @DavidL well obviously because .extend and .append are different!
              – juanpa.arrivillaga
              Dec 27 '18 at 20:22
















            thanks for these answers, these are very helpful. Out of curiosity, why does keys.extend(d) only add the key and not the entire dict there? I actually had to test it to make sure it worked because it seemed so counterintuitive. (Unlike append)
            – David L
            Dec 27 '18 at 20:20






            thanks for these answers, these are very helpful. Out of curiosity, why does keys.extend(d) only add the key and not the entire dict there? I actually had to test it to make sure it worked because it seemed so counterintuitive. (Unlike append)
            – David L
            Dec 27 '18 at 20:20






            1




            1




            .extend takes an iterable, and adds all the elements in the iterable to the list. dict objects are iterables over their keys. If you want an iterable of the values, use d.values(), if you want an iterable of the key-value pairs, use d.items()
            – juanpa.arrivillaga
            Dec 27 '18 at 20:21




            .extend takes an iterable, and adds all the elements in the iterable to the list. dict objects are iterables over their keys. If you want an iterable of the values, use d.values(), if you want an iterable of the key-value pairs, use d.items()
            – juanpa.arrivillaga
            Dec 27 '18 at 20:21












            @DavidL well obviously because .extend and .append are different!
            – juanpa.arrivillaga
            Dec 27 '18 at 20:22




            @DavidL well obviously because .extend and .append are different!
            – juanpa.arrivillaga
            Dec 27 '18 at 20:22













            2














            This is simpler and does the same thing:



            [k for d in e.original_column_data for k in d]
            => ['id', 'number', 'id']





            share|improve this answer




























              2














              This is simpler and does the same thing:



              [k for d in e.original_column_data for k in d]
              => ['id', 'number', 'id']





              share|improve this answer


























                2












                2








                2






                This is simpler and does the same thing:



                [k for d in e.original_column_data for k in d]
                => ['id', 'number', 'id']





                share|improve this answer














                This is simpler and does the same thing:



                [k for d in e.original_column_data for k in d]
                => ['id', 'number', 'id']






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 27 '18 at 20:19

























                answered Dec 27 '18 at 20:18









                Óscar López

                175k22225320




                175k22225320























                    2














                    first way



                    iteration on a dictionary gives u its keys, so a simple



                    >>> [key for key in dict]


                    gives u a list of keys and you can get what you want with



                    >>> [key for dict in dict_list for key in dict]


                    second way



                    use .key() (used in your code)
                    but there is no need to use list() (edit: for python 2)

                    here's what it will look like:



                    >>> [dict.keys()[0] for dict in dict_list]


                    in your code, dictionaries have only one key so these two has the same result.

                    but I prefer the first one since it gives all keys of all the dictionaries






                    share|improve this answer



















                    • 1




                      dict.keys()[0] will not work in Python 3
                      – juanpa.arrivillaga
                      Dec 27 '18 at 21:11
















                    2














                    first way



                    iteration on a dictionary gives u its keys, so a simple



                    >>> [key for key in dict]


                    gives u a list of keys and you can get what you want with



                    >>> [key for dict in dict_list for key in dict]


                    second way



                    use .key() (used in your code)
                    but there is no need to use list() (edit: for python 2)

                    here's what it will look like:



                    >>> [dict.keys()[0] for dict in dict_list]


                    in your code, dictionaries have only one key so these two has the same result.

                    but I prefer the first one since it gives all keys of all the dictionaries






                    share|improve this answer



















                    • 1




                      dict.keys()[0] will not work in Python 3
                      – juanpa.arrivillaga
                      Dec 27 '18 at 21:11














                    2












                    2








                    2






                    first way



                    iteration on a dictionary gives u its keys, so a simple



                    >>> [key for key in dict]


                    gives u a list of keys and you can get what you want with



                    >>> [key for dict in dict_list for key in dict]


                    second way



                    use .key() (used in your code)
                    but there is no need to use list() (edit: for python 2)

                    here's what it will look like:



                    >>> [dict.keys()[0] for dict in dict_list]


                    in your code, dictionaries have only one key so these two has the same result.

                    but I prefer the first one since it gives all keys of all the dictionaries






                    share|improve this answer














                    first way



                    iteration on a dictionary gives u its keys, so a simple



                    >>> [key for key in dict]


                    gives u a list of keys and you can get what you want with



                    >>> [key for dict in dict_list for key in dict]


                    second way



                    use .key() (used in your code)
                    but there is no need to use list() (edit: for python 2)

                    here's what it will look like:



                    >>> [dict.keys()[0] for dict in dict_list]


                    in your code, dictionaries have only one key so these two has the same result.

                    but I prefer the first one since it gives all keys of all the dictionaries







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 29 '18 at 3:43

























                    answered Dec 27 '18 at 20:41









                    SASA1024

                    1227




                    1227








                    • 1




                      dict.keys()[0] will not work in Python 3
                      – juanpa.arrivillaga
                      Dec 27 '18 at 21:11














                    • 1




                      dict.keys()[0] will not work in Python 3
                      – juanpa.arrivillaga
                      Dec 27 '18 at 21:11








                    1




                    1




                    dict.keys()[0] will not work in Python 3
                    – juanpa.arrivillaga
                    Dec 27 '18 at 21:11




                    dict.keys()[0] will not work in Python 3
                    – juanpa.arrivillaga
                    Dec 27 '18 at 21:11



                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas