Is there a way to create a dictionary with a nested list but for specific indices?
I have input:
data = [[int, int, int], [int, int, int], [int, int,int]]
I want to so something along the lines of:
my_dictionary = {}
for x in data:
my_dictionary[x[0]] = []
my_dictionary[x[1]] = []
but without having to iterate through the whole thing.
For example:
data = [[a,b,c], [d,e,f], [g,h,i]]
# would leave me with:
my_dictionary = {a: [] , b:[], d:[], e:[], g:[], h:[] }
Is there a way to specify this using dictionary comprehension?
Just do this:
>>> data
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> {d[i]:[] for i in (0,1) for d in data}
{1: [], 2: [], 4: [], 5: [], 7: [], 8: []}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With