1

My list of lists looks like this:

my_list = [[sub_list_1],[sub_list_2],...,[sub_list_n]]

Desired output

my_dict[1] = [sub_list_1]
my_dict[2] = [sub_list_2]
my_dict[n] = [sub_list_n]

I want the keys for the dictionary to be generated on their own. How can this be achieved in a pythonic way?

I look at certain questions like

but they either provide a list of keys or focus on using some information from the lists as keys.

Alternatively, I tried making a list of keys this way:

my_keys = list(range(len(my_list)))
my_dict = dict(zip(my_keys,my_list)

and it works but, this does not:

my_dict = dict(zip(list(range(len(my_list))),my_list))

This gives me a syntax error.

So in summary:

  • Is there a way to generate a dictionary of lists without explicitly providing keys?, and
  • Why does the combined code throw a syntax error whereas the two step code works?
sheth7
  • 351
  • 2
  • 14
  • More details on the error? `my_dict = dict(zip(list(range(len(my_list))),my_list))` worked just fine for me. – Goldwave Oct 01 '20 at 21:32
  • my_dict = dict(zip(list(range(len(my_list))), my_list)) ^ SyntaxError: invalid syntax – sheth7 Oct 01 '20 at 21:35
  • this worked fine for me as well (copy-pasted from your comment): ```my_list = [["hello1"], ["hello2"]] my_dict = dict(zip(list(range(len(my_list))), my_list)) ``` – 123 Oct 01 '20 at 21:38
  • are you able to provide us with a bit more code? There shouldnt be anything wrong with that line. – Goldwave Oct 01 '20 at 21:38
  • edited question to add error screen shot – sheth7 Oct 01 '20 at 21:43
  • 1
    `try` is a reserved word in python (try-catch statement) – 123 Oct 01 '20 at 21:48
  • See my answer for links to the try/except statement. As @JustinMai mentions, this is a reserved "name" in python, which cannot be assigned to a value. You can use `Try` or `TRY` – Goldwave Oct 01 '20 at 21:51
  • `my_dict = {n: sublist for n, sublist in enumerate(my_list, start=1)}` – Alexander Oct 01 '20 at 21:55
  • Please do not share information as images unless absolutely necessary. See: https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors, https://idownvotedbecau.se/imageofcode, https://idownvotedbecau.se/imageofanexception/. – AMC Oct 02 '20 at 00:36
  • oh sorry, removed the screenshot. – sheth7 Oct 02 '20 at 03:34

4 Answers4

2

I would recommend to use a dict comprehension to achieve what you want like in here, moreover I tried your implementation and haven't faced any issues (more details are more than welcome):

my_list = [["sub_list_1"],["sub_list_2"],["sub_list_3"]]
my_dict = dict(zip(list(range(len(my_list))),my_list))
alternative_dict = {iter:item for iter,item in enumerate(my_list)}
print("yours : " + str(my_dict))
print("mine : " + str(alternative_dict))

output:

yours : {0: ['sub_list_1'], 1: ['sub_list_2'], 2: ['sub_list_3']}
mine : {0: ['sub_list_1'], 1: ['sub_list_2'], 2: ['sub_list_3']}
Yossi Levi
  • 1,258
  • 1
  • 4
  • 7
2

Your syntax error is caused by your variable name try. try is allready a name in python. see try/except

Goldwave
  • 599
  • 3
  • 13
1

I received no error message when running your code:

>>> my_list = [["hello1"], ["hello2"]]
>>> my_dict = dict(zip(list(range(len(my_list))), my_list))
>>> my_dict
{1: ['hello1'], 2: ['hello2']}

You can create a dict of lists from a list of lists using a dict comprehension:

my_dict = {i: sub_list for i, sub_list in enumerate(my_list)}
123
  • 595
  • 6
  • 18
  • > Is there a way to generate a dictionary of lists without explicitly providing keys? – 123 Oct 01 '20 at 21:41
  • he's actually asking both; you answered the syntax error portion and I showed how to make a dict of lists from list of lists without defined keys. – 123 Oct 01 '20 at 21:42
  • 1
    Yeah, I read the question again. Please excuse my mistake. Although you didn't really provide any explanation for your code either. – Goldwave Oct 01 '20 at 21:43
  • no worries we're humans. and Yossi's answer has both the syntax error portion and the implementation portion of the question so it's technically more correct. i just don't like how the single line of code needed is buried in other explanatory lines but oh well – 123 Oct 01 '20 at 21:45
  • 1
    I understand, I just believe there is more value in providing OP with the knowledge of answering the question as well as the answer. EDIT: Thank you for your edit! Looks great – Goldwave Oct 01 '20 at 21:46
  • 1
    Hi Justin, i did not downvote. Maybe someone else. I just want to get over with this and sleep ;) – sheth7 Oct 01 '20 at 21:49
1

This should do it

my_dict = {my_list.index(i) + 1: i for i in my_list}

Notice that I have added +1 to start at the key 1 instead of 0 to match your expectations

Concorde
  • 87
  • 3
  • i believe using `.index()` will be slower than `enumerate(list)` – 123 Oct 01 '20 at 21:40
  • 1
    That is the contrally. Enumerate checks for the index and value at the same time. Hence the `.index` solution is 1/3 faster than using `enumerate` as the latter solution checks the value 2 times. – Concorde Oct 01 '20 at 21:47
  • oh! interesting, thank you. what's the name of the "law" where you'll find the correct answer quickest by publishing the incorrect answer? :) – 123 Oct 01 '20 at 21:52