Single line for-loop to build a dictionary?

You can use a dict comprehension:

data = {smallItem:smallItem for smallItem in bigList}

You might also use dict and a generator expression:

data = dict((smallItem, smallItem) for smallItem in bigList)

But the dict comprehension will be faster.

As for converting this into a JSON string, you can use json.dumps.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)