Creating a list of lists into a dictionary [duplicate]
up vote
-5
down vote
favorite
This question already has an answer here:
Creating a dictionary with list of lists in Python
3 answers
I'm trying to find a code that transforms a list of lists into a dictionary. Lets say I have a list that is
list_one = [['id1', 'id2', id3', 'id4', 'id5'],
['1', 'Cat', '400', 'Fur', '50'],
['2', 'Dog', '500', 'Smelly', '60']]
The dictionary should have keys to number each list in dictionaries in this format
new_dict = {1.0: {'id1': 1,
'id2': 'Cat',
'id3': 400,
'id4': 'Fur',
'id5': 50},
2.0: {'id1': 2,
'id2': 'Dog',
'id3': 500,
'id4': 'Smelly'
'id5': 60}
Can such a conversion be done in list comprehension or through a for loop?
python list dictionary for-loop
marked as duplicate by Iguananaut, Sufiyan Ghori, usr2564301, Prune
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();
}
);
});
});
Nov 7 at 22:02
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 |
up vote
-5
down vote
favorite
This question already has an answer here:
Creating a dictionary with list of lists in Python
3 answers
I'm trying to find a code that transforms a list of lists into a dictionary. Lets say I have a list that is
list_one = [['id1', 'id2', id3', 'id4', 'id5'],
['1', 'Cat', '400', 'Fur', '50'],
['2', 'Dog', '500', 'Smelly', '60']]
The dictionary should have keys to number each list in dictionaries in this format
new_dict = {1.0: {'id1': 1,
'id2': 'Cat',
'id3': 400,
'id4': 'Fur',
'id5': 50},
2.0: {'id1': 2,
'id2': 'Dog',
'id3': 500,
'id4': 'Smelly'
'id5': 60}
Can such a conversion be done in list comprehension or through a for loop?
python list dictionary for-loop
marked as duplicate by Iguananaut, Sufiyan Ghori, usr2564301, Prune
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();
}
);
});
});
Nov 7 at 22:02
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.
6
Seems very simple, so why not try implementing it yourself instead of spending potentially more time looking for ready-made code online?
– Roope
Nov 7 at 21:40
1
in our target dict, why are the keys1.0 ,2.0etc. Why decimals? what is the physical significance?
– Srini
Nov 7 at 21:42
1
idownvotedbecau.se/noattempt
– Roope
Nov 7 at 21:45
To @Srini 's point, storing the keys asfloatsincurs extra overhead for no particular reason. Just store asint, it is smaller (from a data perspective) and makes it a bit easier to look up
– C.Nivs
Nov 7 at 21:51
add a comment |
up vote
-5
down vote
favorite
up vote
-5
down vote
favorite
This question already has an answer here:
Creating a dictionary with list of lists in Python
3 answers
I'm trying to find a code that transforms a list of lists into a dictionary. Lets say I have a list that is
list_one = [['id1', 'id2', id3', 'id4', 'id5'],
['1', 'Cat', '400', 'Fur', '50'],
['2', 'Dog', '500', 'Smelly', '60']]
The dictionary should have keys to number each list in dictionaries in this format
new_dict = {1.0: {'id1': 1,
'id2': 'Cat',
'id3': 400,
'id4': 'Fur',
'id5': 50},
2.0: {'id1': 2,
'id2': 'Dog',
'id3': 500,
'id4': 'Smelly'
'id5': 60}
Can such a conversion be done in list comprehension or through a for loop?
python list dictionary for-loop
This question already has an answer here:
Creating a dictionary with list of lists in Python
3 answers
I'm trying to find a code that transforms a list of lists into a dictionary. Lets say I have a list that is
list_one = [['id1', 'id2', id3', 'id4', 'id5'],
['1', 'Cat', '400', 'Fur', '50'],
['2', 'Dog', '500', 'Smelly', '60']]
The dictionary should have keys to number each list in dictionaries in this format
new_dict = {1.0: {'id1': 1,
'id2': 'Cat',
'id3': 400,
'id4': 'Fur',
'id5': 50},
2.0: {'id1': 2,
'id2': 'Dog',
'id3': 500,
'id4': 'Smelly'
'id5': 60}
Can such a conversion be done in list comprehension or through a for loop?
This question already has an answer here:
Creating a dictionary with list of lists in Python
3 answers
python list dictionary for-loop
python list dictionary for-loop
asked Nov 7 at 21:38
user1821176
3141420
3141420
marked as duplicate by Iguananaut, Sufiyan Ghori, usr2564301, Prune
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();
}
);
});
});
Nov 7 at 22:02
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 Iguananaut, Sufiyan Ghori, usr2564301, Prune
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();
}
);
});
});
Nov 7 at 22:02
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.
6
Seems very simple, so why not try implementing it yourself instead of spending potentially more time looking for ready-made code online?
– Roope
Nov 7 at 21:40
1
in our target dict, why are the keys1.0 ,2.0etc. Why decimals? what is the physical significance?
– Srini
Nov 7 at 21:42
1
idownvotedbecau.se/noattempt
– Roope
Nov 7 at 21:45
To @Srini 's point, storing the keys asfloatsincurs extra overhead for no particular reason. Just store asint, it is smaller (from a data perspective) and makes it a bit easier to look up
– C.Nivs
Nov 7 at 21:51
add a comment |
6
Seems very simple, so why not try implementing it yourself instead of spending potentially more time looking for ready-made code online?
– Roope
Nov 7 at 21:40
1
in our target dict, why are the keys1.0 ,2.0etc. Why decimals? what is the physical significance?
– Srini
Nov 7 at 21:42
1
idownvotedbecau.se/noattempt
– Roope
Nov 7 at 21:45
To @Srini 's point, storing the keys asfloatsincurs extra overhead for no particular reason. Just store asint, it is smaller (from a data perspective) and makes it a bit easier to look up
– C.Nivs
Nov 7 at 21:51
6
6
Seems very simple, so why not try implementing it yourself instead of spending potentially more time looking for ready-made code online?
– Roope
Nov 7 at 21:40
Seems very simple, so why not try implementing it yourself instead of spending potentially more time looking for ready-made code online?
– Roope
Nov 7 at 21:40
1
1
in our target dict, why are the keys
1.0 ,2.0 etc. Why decimals? what is the physical significance?– Srini
Nov 7 at 21:42
in our target dict, why are the keys
1.0 ,2.0 etc. Why decimals? what is the physical significance?– Srini
Nov 7 at 21:42
1
1
idownvotedbecau.se/noattempt
– Roope
Nov 7 at 21:45
idownvotedbecau.se/noattempt
– Roope
Nov 7 at 21:45
To @Srini 's point, storing the keys as
floats incurs extra overhead for no particular reason. Just store as int, it is smaller (from a data perspective) and makes it a bit easier to look up– C.Nivs
Nov 7 at 21:51
To @Srini 's point, storing the keys as
floats incurs extra overhead for no particular reason. Just store as int, it is smaller (from a data perspective) and makes it a bit easier to look up– C.Nivs
Nov 7 at 21:51
add a comment |
4 Answers
4
active
oldest
votes
up vote
3
down vote
accepted
a simple dict-comprehension using enumerate:
list_one = [['id1', 'id2', 'id3', 'id4', 'id5'],
['1', 'Cat', '400', 'Fur', '50'],
['2', 'Dog', '500', 'Smelly', '60']]
new_dict = {float(i): dict(zip(list_one[0], items))
for i, items in enumerate(list_one[1:], start=1)}
print(new_dict)
results in
{1.0: {'id1': '1', 'id2': 'Cat', 'id3': '400', 'id4': 'Fur', 'id5': '50'},
2.0: {'id1': '2', 'id2': 'Dog', 'id3': '500', 'id4': 'Smelly', 'id5': '60'}}
add a comment |
up vote
2
down vote
In your use case, the keys are the first list in your list, and the objects are the rest. You can zip them together and create that dictionary like so:
ks, objs = list_one[0], list_one[1:]
myobjects = {i: dict(zip(ks, l)) for i,l in enumerate(objs)}
zip will take the element-by-element pairs and create tuples like (x, y) and dict will make {x: y} from that tuple. The docs can be found here
add a comment |
up vote
2
down vote
If you are okay with third party library pandas
import pandas as pd
{float(k):v for k,v in enumerate(pd.DataFrame(list_one[1:],columns=list_one[0]).to_dict('records'),1)}
1
op said nothing about using pandas
– Srini
Nov 7 at 21:44
3
@Srini to be fair: the OP said nothing against using pandas....
– hiro protagonist
Nov 7 at 21:45
@hiroprotagonist true. But the original version of the answer did not make it clear that pandas was being used either. I just wanted to highlight that too :)
– Srini
Nov 7 at 21:47
add a comment |
up vote
1
down vote
The following should do the trick:
>>> new_dict = {float(i): {list_one[0][j]:val for j,val in enumerate(list_one[i])} for i in range(1,len(list_one))}
>>> new_dict
{1.0: {'id1': '1', 'id2': 'Cat', 'id3': '400', 'id4': 'Fur', 'id5': '50'},
2.0: {'id1': '2', 'id2': 'Dog', 'id3': '500', 'id4': 'Smelly', 'id5': '60'}}
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
a simple dict-comprehension using enumerate:
list_one = [['id1', 'id2', 'id3', 'id4', 'id5'],
['1', 'Cat', '400', 'Fur', '50'],
['2', 'Dog', '500', 'Smelly', '60']]
new_dict = {float(i): dict(zip(list_one[0], items))
for i, items in enumerate(list_one[1:], start=1)}
print(new_dict)
results in
{1.0: {'id1': '1', 'id2': 'Cat', 'id3': '400', 'id4': 'Fur', 'id5': '50'},
2.0: {'id1': '2', 'id2': 'Dog', 'id3': '500', 'id4': 'Smelly', 'id5': '60'}}
add a comment |
up vote
3
down vote
accepted
a simple dict-comprehension using enumerate:
list_one = [['id1', 'id2', 'id3', 'id4', 'id5'],
['1', 'Cat', '400', 'Fur', '50'],
['2', 'Dog', '500', 'Smelly', '60']]
new_dict = {float(i): dict(zip(list_one[0], items))
for i, items in enumerate(list_one[1:], start=1)}
print(new_dict)
results in
{1.0: {'id1': '1', 'id2': 'Cat', 'id3': '400', 'id4': 'Fur', 'id5': '50'},
2.0: {'id1': '2', 'id2': 'Dog', 'id3': '500', 'id4': 'Smelly', 'id5': '60'}}
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
a simple dict-comprehension using enumerate:
list_one = [['id1', 'id2', 'id3', 'id4', 'id5'],
['1', 'Cat', '400', 'Fur', '50'],
['2', 'Dog', '500', 'Smelly', '60']]
new_dict = {float(i): dict(zip(list_one[0], items))
for i, items in enumerate(list_one[1:], start=1)}
print(new_dict)
results in
{1.0: {'id1': '1', 'id2': 'Cat', 'id3': '400', 'id4': 'Fur', 'id5': '50'},
2.0: {'id1': '2', 'id2': 'Dog', 'id3': '500', 'id4': 'Smelly', 'id5': '60'}}
a simple dict-comprehension using enumerate:
list_one = [['id1', 'id2', 'id3', 'id4', 'id5'],
['1', 'Cat', '400', 'Fur', '50'],
['2', 'Dog', '500', 'Smelly', '60']]
new_dict = {float(i): dict(zip(list_one[0], items))
for i, items in enumerate(list_one[1:], start=1)}
print(new_dict)
results in
{1.0: {'id1': '1', 'id2': 'Cat', 'id3': '400', 'id4': 'Fur', 'id5': '50'},
2.0: {'id1': '2', 'id2': 'Dog', 'id3': '500', 'id4': 'Smelly', 'id5': '60'}}
answered Nov 7 at 21:43
hiro protagonist
17.8k63660
17.8k63660
add a comment |
add a comment |
up vote
2
down vote
In your use case, the keys are the first list in your list, and the objects are the rest. You can zip them together and create that dictionary like so:
ks, objs = list_one[0], list_one[1:]
myobjects = {i: dict(zip(ks, l)) for i,l in enumerate(objs)}
zip will take the element-by-element pairs and create tuples like (x, y) and dict will make {x: y} from that tuple. The docs can be found here
add a comment |
up vote
2
down vote
In your use case, the keys are the first list in your list, and the objects are the rest. You can zip them together and create that dictionary like so:
ks, objs = list_one[0], list_one[1:]
myobjects = {i: dict(zip(ks, l)) for i,l in enumerate(objs)}
zip will take the element-by-element pairs and create tuples like (x, y) and dict will make {x: y} from that tuple. The docs can be found here
add a comment |
up vote
2
down vote
up vote
2
down vote
In your use case, the keys are the first list in your list, and the objects are the rest. You can zip them together and create that dictionary like so:
ks, objs = list_one[0], list_one[1:]
myobjects = {i: dict(zip(ks, l)) for i,l in enumerate(objs)}
zip will take the element-by-element pairs and create tuples like (x, y) and dict will make {x: y} from that tuple. The docs can be found here
In your use case, the keys are the first list in your list, and the objects are the rest. You can zip them together and create that dictionary like so:
ks, objs = list_one[0], list_one[1:]
myobjects = {i: dict(zip(ks, l)) for i,l in enumerate(objs)}
zip will take the element-by-element pairs and create tuples like (x, y) and dict will make {x: y} from that tuple. The docs can be found here
answered Nov 7 at 21:43
C.Nivs
1,6781414
1,6781414
add a comment |
add a comment |
up vote
2
down vote
If you are okay with third party library pandas
import pandas as pd
{float(k):v for k,v in enumerate(pd.DataFrame(list_one[1:],columns=list_one[0]).to_dict('records'),1)}
1
op said nothing about using pandas
– Srini
Nov 7 at 21:44
3
@Srini to be fair: the OP said nothing against using pandas....
– hiro protagonist
Nov 7 at 21:45
@hiroprotagonist true. But the original version of the answer did not make it clear that pandas was being used either. I just wanted to highlight that too :)
– Srini
Nov 7 at 21:47
add a comment |
up vote
2
down vote
If you are okay with third party library pandas
import pandas as pd
{float(k):v for k,v in enumerate(pd.DataFrame(list_one[1:],columns=list_one[0]).to_dict('records'),1)}
1
op said nothing about using pandas
– Srini
Nov 7 at 21:44
3
@Srini to be fair: the OP said nothing against using pandas....
– hiro protagonist
Nov 7 at 21:45
@hiroprotagonist true. But the original version of the answer did not make it clear that pandas was being used either. I just wanted to highlight that too :)
– Srini
Nov 7 at 21:47
add a comment |
up vote
2
down vote
up vote
2
down vote
If you are okay with third party library pandas
import pandas as pd
{float(k):v for k,v in enumerate(pd.DataFrame(list_one[1:],columns=list_one[0]).to_dict('records'),1)}
If you are okay with third party library pandas
import pandas as pd
{float(k):v for k,v in enumerate(pd.DataFrame(list_one[1:],columns=list_one[0]).to_dict('records'),1)}
edited Nov 7 at 21:52
answered Nov 7 at 21:43
mad_
3,1211920
3,1211920
1
op said nothing about using pandas
– Srini
Nov 7 at 21:44
3
@Srini to be fair: the OP said nothing against using pandas....
– hiro protagonist
Nov 7 at 21:45
@hiroprotagonist true. But the original version of the answer did not make it clear that pandas was being used either. I just wanted to highlight that too :)
– Srini
Nov 7 at 21:47
add a comment |
1
op said nothing about using pandas
– Srini
Nov 7 at 21:44
3
@Srini to be fair: the OP said nothing against using pandas....
– hiro protagonist
Nov 7 at 21:45
@hiroprotagonist true. But the original version of the answer did not make it clear that pandas was being used either. I just wanted to highlight that too :)
– Srini
Nov 7 at 21:47
1
1
op said nothing about using pandas
– Srini
Nov 7 at 21:44
op said nothing about using pandas
– Srini
Nov 7 at 21:44
3
3
@Srini to be fair: the OP said nothing against using pandas....
– hiro protagonist
Nov 7 at 21:45
@Srini to be fair: the OP said nothing against using pandas....
– hiro protagonist
Nov 7 at 21:45
@hiroprotagonist true. But the original version of the answer did not make it clear that pandas was being used either. I just wanted to highlight that too :)
– Srini
Nov 7 at 21:47
@hiroprotagonist true. But the original version of the answer did not make it clear that pandas was being used either. I just wanted to highlight that too :)
– Srini
Nov 7 at 21:47
add a comment |
up vote
1
down vote
The following should do the trick:
>>> new_dict = {float(i): {list_one[0][j]:val for j,val in enumerate(list_one[i])} for i in range(1,len(list_one))}
>>> new_dict
{1.0: {'id1': '1', 'id2': 'Cat', 'id3': '400', 'id4': 'Fur', 'id5': '50'},
2.0: {'id1': '2', 'id2': 'Dog', 'id3': '500', 'id4': 'Smelly', 'id5': '60'}}
add a comment |
up vote
1
down vote
The following should do the trick:
>>> new_dict = {float(i): {list_one[0][j]:val for j,val in enumerate(list_one[i])} for i in range(1,len(list_one))}
>>> new_dict
{1.0: {'id1': '1', 'id2': 'Cat', 'id3': '400', 'id4': 'Fur', 'id5': '50'},
2.0: {'id1': '2', 'id2': 'Dog', 'id3': '500', 'id4': 'Smelly', 'id5': '60'}}
add a comment |
up vote
1
down vote
up vote
1
down vote
The following should do the trick:
>>> new_dict = {float(i): {list_one[0][j]:val for j,val in enumerate(list_one[i])} for i in range(1,len(list_one))}
>>> new_dict
{1.0: {'id1': '1', 'id2': 'Cat', 'id3': '400', 'id4': 'Fur', 'id5': '50'},
2.0: {'id1': '2', 'id2': 'Dog', 'id3': '500', 'id4': 'Smelly', 'id5': '60'}}
The following should do the trick:
>>> new_dict = {float(i): {list_one[0][j]:val for j,val in enumerate(list_one[i])} for i in range(1,len(list_one))}
>>> new_dict
{1.0: {'id1': '1', 'id2': 'Cat', 'id3': '400', 'id4': 'Fur', 'id5': '50'},
2.0: {'id1': '2', 'id2': 'Dog', 'id3': '500', 'id4': 'Smelly', 'id5': '60'}}
answered Nov 7 at 21:43
Tim
860413
860413
add a comment |
add a comment |
6
Seems very simple, so why not try implementing it yourself instead of spending potentially more time looking for ready-made code online?
– Roope
Nov 7 at 21:40
1
in our target dict, why are the keys
1.0 ,2.0etc. Why decimals? what is the physical significance?– Srini
Nov 7 at 21:42
1
idownvotedbecau.se/noattempt
– Roope
Nov 7 at 21:45
To @Srini 's point, storing the keys as
floatsincurs extra overhead for no particular reason. Just store asint, it is smaller (from a data perspective) and makes it a bit easier to look up– C.Nivs
Nov 7 at 21:51