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?

2
  • 4
    You need to use curly rather than square brackets, but otherwise this is probably as good as it gets. Commented Jan 4, 2015 at 22:05
  • 2
    Plus one for laconic as a synonym for pythonic :-) Commented Nov 2, 2018 at 7:54

6 Answers 6

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} 
Sign up to request clarification or add additional context in comments.

Comments

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]} 

Comments

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]} 

Comments

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} 

Comments

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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.