7

Which is the best way to make a dictionary of lists? For instance, if I have lists list1, list2 and want to make a dictionary my_dict like that:

my_dict = ['list1': list1, 'list2': list2]

I've found this example but the best answer is written in 2009. Maybe there are some new more laconic ways to do this?

Community
  • 1
  • 1
Elena
  • 149
  • 1
  • 3
  • 13

6 Answers6

9

You need to use curly rather than square brackets, but otherwise this is probably as good as it gets:

list1 = ['a', 'b', 'c']
list2 = [1, 2, 3, 4]
my_dict = {'list1': list1, 'list2': list2}
NPE
  • 486,780
  • 108
  • 951
  • 1,012
4

For a dictionary of lists, consider a defaultdict.

A normal dictionary works fine, but it raises an error if a key is not found.

list1 = list("abcd")
list2 = [1, 2, 3, 4]


d = {"list1": list1, "list2": list2}
d["list3"]
# KeyError: 'list3'

This may be disruptive in some applications and may require additional exception handling.

The defaultdict behaves like a normal dict while adding some protection against errors.

import collections as ct


dd = ct.defaultdict(list)
dd.update(d)
dd
# defaultdict(list, {'list1': ['a', 'b', 'c', 'd'], 'list2': [1, 2, 3, 4]})

Adding a missing key will call the default factory function, i.e. list. Here instead of a error, we get an empty container:

dd["list3"]
# []

This entry was added with an empty list.

dd
# defaultdict(list,
#             {'list1': ['a', 'b', 'c', 'd'],
#              'list2': [1, 2, 3, 4],
#              'list3': []})

Convert a defaultdict to a regular dict by setting the default factory to None

dd.default_factory = None
dd
# defaultdict(None, {'list1': ['a', 'b', 'c', 'd'], 'list2': [1, 2, 3, 4]})

or by using the dict() builtin:

dict(dd)
# {'list1': ['a', 'b', 'c', 'd'], 'list2': [1, 2, 3, 4]}
pylang
  • 40,867
  • 14
  • 129
  • 121
2

If you want to turn the variable name into a key, here is a similar question.

If you just want a dictionary of lists with a sequential key.

def turn_to_dict(*args):
    return {i: v for i, v in enumerate(args)}

lst1 = [1, 2, 3, 4]
lst2 = [3, 4, 6, 7]
lst3 = [5, 8, 9]

v = turn_to_dict(lst1, lst2, lst3)

>>> print(v)
{0: [1, 2, 3, 4], 1: [3, 4, 6, 7], 2: [5, 8, 9]} 
0 _
  • 10,524
  • 11
  • 77
  • 109
GGhe
  • 663
  • 5
  • 17
1

Try this method, very succinct. Curly braces, not square brackets.

I think that's the shortest way around it.

list1 = [5, 500, 543]
list2 = [4, 4, 4]

my_dict = {'list1':list1, 'list2': list2}
Orange Mushroom
  • 431
  • 4
  • 16
0

Use the curly brace syntax to define the dictionary, and give each entry in your dictionary a key that corresponds to each value:

list_a = [1,2,3,4,5]
list_b = [6,7,8,9,10]

my_dict = {'list1':list_a, 'list2':list_b}

More in the python docs

msturdy
  • 10,479
  • 11
  • 41
  • 52
0

This should work:

my_dict = dict([('list1', list1), ('list2', list2)])

Or, alternatively:

my_dict = {'list1': list1, 'list2': list2}

The result will be the same.

Ashalynd
  • 12,363
  • 2
  • 34
  • 37