Getting “keys” from list of dicts [duplicate]
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
python
marked as duplicate by jpp
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.
add a comment |
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
python
marked as duplicate by jpp
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.
add a comment |
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
python
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
python
asked Dec 27 '18 at 20:14
David L
30213
30213
marked as duplicate by jpp
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
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.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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']
thanks for these answers, these are very helpful. Out of curiosity, why doeskeys.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. (Unlikeappend
)
– 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, used.values()
, if you want an iterable of the key-value pairs, used.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
add a comment |
This is simpler and does the same thing:
[k for d in e.original_column_data for k in d]
=> ['id', 'number', 'id']
add a comment |
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
1
dict.keys()[0]
will not work in Python 3
– juanpa.arrivillaga
Dec 27 '18 at 21:11
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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']
thanks for these answers, these are very helpful. Out of curiosity, why doeskeys.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. (Unlikeappend
)
– 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, used.values()
, if you want an iterable of the key-value pairs, used.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
add a comment |
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']
thanks for these answers, these are very helpful. Out of curiosity, why doeskeys.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. (Unlikeappend
)
– 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, used.values()
, if you want an iterable of the key-value pairs, used.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
add a comment |
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']
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']
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 doeskeys.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. (Unlikeappend
)
– 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, used.values()
, if you want an iterable of the key-value pairs, used.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
add a comment |
thanks for these answers, these are very helpful. Out of curiosity, why doeskeys.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. (Unlikeappend
)
– 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, used.values()
, if you want an iterable of the key-value pairs, used.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
add a comment |
This is simpler and does the same thing:
[k for d in e.original_column_data for k in d]
=> ['id', 'number', 'id']
add a comment |
This is simpler and does the same thing:
[k for d in e.original_column_data for k in d]
=> ['id', 'number', 'id']
add a comment |
This is simpler and does the same thing:
[k for d in e.original_column_data for k in d]
=> ['id', 'number', 'id']
This is simpler and does the same thing:
[k for d in e.original_column_data for k in d]
=> ['id', 'number', 'id']
edited Dec 27 '18 at 20:19
answered Dec 27 '18 at 20:18
Óscar López
175k22225320
175k22225320
add a comment |
add a comment |
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
1
dict.keys()[0]
will not work in Python 3
– juanpa.arrivillaga
Dec 27 '18 at 21:11
add a comment |
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
1
dict.keys()[0]
will not work in Python 3
– juanpa.arrivillaga
Dec 27 '18 at 21:11
add a comment |
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
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
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
add a comment |
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
add a comment |