You can use defaultdict:

>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> a = ['1', '2']
>>> for i in a:
...   for j in range(int(i), int(i) + 2):
...     d[j].append(i)
...
>>> d
defaultdict(<type 'list'>, {1: ['1'], 2: ['1', '2'], 3: ['2']})
>>> d.items()
[(1, ['1']), (2, ['1', '2']), (3, ['2'])]
Answer from mechanical_meat on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › tutorial › datastructures.html
5. Data Structures — Python 3.14.7 documentation
To avoid getting this error when ... the key is not in the dictionary. Performing list(d) on a dictionary returns a list of all the keys used in the dictionary, in insertion order (if you want it sorted, just use sorted(d) ...
Discussions

Why not always use a dictionary instead of a list?
If you find that easier, then by all means do it. However just know I believe dictionaries are a lot faster than lists, Is completely wrong. dictionaries are slightly slower than lists, as they require a hash call. This speed difference and the memory difference you mentioned are negligible, so just use whatever floats your boat. More on reddit.com
🌐 r/learnpython
16
6
September 14, 2020
dictionary - Creating a list of dictionaries in python - Stack Overflow
You actually have a list of strings, and you'd like to have a list of paired dictionaries generated from the same key in the tuple triplets of each string. To keep this relatively simple, I'll use a for loop instead of a complicated dictionary comprehension structure. More on stackoverflow.com
🌐 stackoverflow.com
Is a list basically a simplified dictionary?
No, it isn't. A dictionary is a hash, a list is an array. For example, you can't have a[0] and a[2] in a list, without a a[1]....but you can with a dictionary. You also cannot just assign b[1]=0 unless it already exists from another operation. >>> a={} >>> a[0]=1 >>> a[2]=1 >>> b=[] >>> b[0]=1 Traceback (most recent call last): File "", line 1, in IndexError: list assignment index out of range More on reddit.com
🌐 r/learnpython
13
0
March 3, 2023
How to turn a list of lists into a dictionary where the first item of each sublist is used as key?
We can use tuple assignment with packing inside a dictionary comprehension, eg. data = [['James', '100.00', '90.00', '85.50'], ['Nick', '78.00', '85.00', '80.50'], ['William', '95.50', '92.00', '100.00']] data_dict = {name: values for name, *values in data} More on reddit.com
🌐 r/learnpython
5
4
November 28, 2022
🌐
W3Schools
w3schools.com › python › python_dictionaries.asp
Python Dictionaries
Remove List Duplicates Reverse a String Add Two Numbers · Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... Dictionaries are used to store data values in key:value pairs.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-ways-to-create-a-dictionary-of-lists
Ways to create a dictionary of Lists - Python - GeeksforGeeks
July 11, 2025 - DSA Python · Data Science · NumPy · Pandas · Practice · Django · Flask · Last Updated : 11 Jul, 2025 · A dictionary of lists is a type of dictionary where each value is a list.
🌐
Enki
enki.com › post › list-and-dict-in-python
Enki | Blog - Difference Between list and dict in Python
For quick data look-ups, configurations, or caches, favor dictionaries. For ordered collections and sequence operations, such as maintaining a stack or queue, lists are more suitable. Don't just learn Python—master it with Enki's tailored courses. Gain in-depth knowledge of lists, dictionaries, and more while boosting your learning speed by 10x.
🌐
Medium
medium.com › @atatus › https-www-atatus-com-blog-python-converting-lsts-to-dictionaries-c3f038a8ce30
Python: Converting Lists to Dictionaries | by Atatus | Medium
September 16, 2024 - Python List is simply an ordered collection of elements, which can be of any data type, such as integers, strings, or even other lists. Lists are defined using square brackets []. Here’s an example of a Python List that contains three integers: ...
Find elsewhere
🌐
New York University
physics.nyu.edu › pine › pymanual › html › chap3 › chap3_arrays.html
3. Strings, Lists, Arrays, and Dictionaries — PyMan 0.9.31 documentation
A Python list is a collection of Python objects indexed by an ordered sequence of integers starting from zero. A dictionary is also collection of Python objects, just like a list, but one that is indexed by strings or numbers (not necessarily integers and not in any particular order) or even tuples!
🌐
Scaler
scaler.com › home › topics › list of dictionaries in python
List of Dictionaries in Python - Scaler Topics
March 31, 2024 - Here, we create a list of dictionaries ls in Python. After that we take another dictionary dict and append it, using "append" in python, to the end of the original dictionary. ... There is no such exception when it comes to appending a dictionary to a list of dictionaries in Python.
🌐
Built In
builtin.com › software-engineering-perspectives › convert-list-to-dictionary-python
10 Ways to Convert Lists to Dictionaries in Python | Built In
By using collections.ChainMap(), we can convert a list of dictionaries to a single dictionary. “ChainMap: A ChainMap groups multiple dicts or other mappings together to create a single, updateable view,” according to Python’s documentation.
🌐
Reddit
reddit.com › r/learnpython › why not always use a dictionary instead of a list?
r/learnpython on Reddit: Why not always use a dictionary instead of a list?
September 14, 2020 -

I believe dictionaries are a lot faster than lists, then why wouldn't you always want to use dictionaries? For example getting an item from a list requires its index, but we could set the "index" to be a key in the dictionary. The only downside to this I can see is more memory consumption and not being able to use list methods. Am I right?

🌐
GeeksforGeeks
geeksforgeeks.org › python › difference-between-list-and-dictionary-in-python
Difference between List and Dictionary in Python - GeeksforGeeks
July 12, 2025 - Lists and Dictionaries in Python are inbuilt data structures that are used to store data. Lists are linear in nature whereas dictionaries stored the data in key-value pairs.
Top answer
1 of 4
7

A one liner, just for fun:

all_examples = ['A,1,1', 'B,2,1', 'C,4,4', 'D,4,5']

map(dict, zip(*[[(s[0], int(x)) for x in s.split(',')[1:]] for s in all_examples]))

Produces:

[{'A': 1, 'C': 4, 'B': 2, 'D': 4}, 
 {'A': 1, 'C': 4, 'B': 1, 'D': 5}]

As a bonus, this will work for longer sequences too:

all_examples = ['A,1,1,1', 'B,2,1,2', 'C,4,4,3', 'D,4,5,6']

Output:

[{'A': 1, 'C': 4, 'B': 2, 'D': 4},
 {'A': 1, 'C': 4, 'B': 1, 'D': 5},
 {'A': 1, 'C': 3, 'B': 2, 'D': 6}]

Explanation:

map(dict, zip(*[[(s[0], int(x)) for x in s.split(',')[1:]] for s in all_examples]))
  • [... for s in all_examples] For each element in your list:
  • s.split(',')[1:] Split it by commas, then take each element after the first
  • (...) for x in and turn it into a list of tuples
  • s[0], int(x) of the first letter, with that element converted to integer
  • zip(*[...]) now transpose your lists of tuples
  • map(dict, ...) and turn each one into a dictionary!
2 of 4
4

Also just for fun, but with a focus on understandability:

all_examples = ['A,1,1', 'B,2,1', 'C,4,4', 'D,4,5']
ll = [ x.split(",") for x in all_examples ]
ld = list()
for col in range(1, len(ll[0])):
    ld.append({ l[0] : int(l[col]) for l in ll })
print ld

will print

[{'A': 1, 'C': 4, 'B': 2, 'D': 4}, {'A': 1, 'C': 4, 'B': 1, 'D': 5}]

Works as long as the input is csv with integers and lines are same length.

Dissection: I will use the teminology "thing" for A, B and C and "measurement" for the "columns" in the data, i.e. those values in the same "csv-column" of the inut data.

Get the string input data into a list for each line: A,1,1 -> ["A","1","1"]

ll = [ x.split(",") for x in all_examples ]

The result is supposed to be a list of dicts, so let's initialize one:

ld = list()

For each measurement (assuming that all lines have the same number of columns):

for col in range(1, len(ll[0])):

Take the thing l[0], e.g. "A", from the line and assign the numeric value int(), e.g. 1, of the measurement in the respective column l[col], e.g. "1", to the thing. Then use a dictionary comprehension to combine it into the next line of the desired result. Finally append() the dict to the result list ld.

    ld.append({ l[0] : int(l[col]) for l in ll })

View unfoamtted. Use print json.dumps(ld, indent=4) for more convenient display:

print ld

Hope this helps. Find more on dict comprehensions e.g. here (Python3 version of this great book).

🌐
Google
developers.google.com › google for education › python › python dict and file
Python Dict and File | Python Education | Google for Developers
For example, you might read a log file where each line begins with an IP address, and store the data into a dict using the IP address as the key, and the list of lines where it appears as the value. Once you've read in the whole file, you can look up any IP address and instantly see its list of lines. The dictionary takes in scattered data and makes it into something coherent.
🌐
Real Python
realpython.com › python-dicts
Dictionaries in Python – Real Python
April 8, 2026 - Using the key and the index, you can access items in nested lists. Similarly, using the outer and inner keys, you can access values in nested dictionaries. Then, the nesting level will define how many keys or indices you’ll have to use. Python dictionaries are dynamically sized data structures.
🌐
Scaleway
scaleway.com › en › docs › tutorials › python-lists-dicts
Getting started with Python lists and dictionaries | Scaleway Documentation
April 22, 2025 - Download the Python lists cheatsheet - Printable (PDF). While a simple list may be sufficient for storing values and carrying out operations like finding the average, what if we wanted to have a record not just of the class marks, but of which students got which marks? That's where dictionaries come in.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-accessing-items-in-lists-within-dictionary
Python - Accessing Items in Lists Within Dictionary - GeeksforGeeks
April 27, 2023 - Step - 2: Next, as the dictionary consists of values which are of type lists so there must be more than one item in the list which we have to fetch and print. This is why we will again iterate over the second variable we took earlier to fetch each element of that particular key and print it. Step - 3 : After printing all the elements of a certain key we will print something to separate it with the others like a line of dash(-) or dots(.). ... # Python program to fetch # items from a list which acts # as a value of dictionary # defining the dictionary country = { "India": ["Delhi", "Maharashtra
🌐
Medium
medium.com › @cssjhnnamae › use-cases-of-python-lists-of-dictionaries-df6f32a4dba0
Use Cases of Python Lists of Dictionaries | by Princess Rodiel | Medium
September 17, 2024 - This example shows how to add a new dictionary to an existing list of dictionaries (ls). Here’s what happens: ... dict_to_append: This is a new dictionary { ‘language’: ‘Python’, ‘Framework’: ‘Django’ } that we want to add to ls.
🌐
Reddit
reddit.com › r/learnpython › is a list basically a simplified dictionary?
r/learnpython on Reddit: Is a list basically a simplified dictionary?
March 3, 2023 -

Would it be accurate to think of a list as a dictionary where the index number is the key that lets you draw up the associated value?

Top answer
1 of 5
11
No, it isn't. A dictionary is a hash, a list is an array. For example, you can't have a[0] and a[2] in a list, without a a[1]....but you can with a dictionary. You also cannot just assign b[1]=0 unless it already exists from another operation. >>> a={} >>> a[0]=1 >>> a[2]=1 >>> b=[] >>> b[0]=1 Traceback (most recent call last): File "", line 1, in IndexError: list assignment index out of range
2 of 5
3
It's actually more like the opposite! In software, a "list" is fundamentally a contiguous block of memory. Meaning at memory address 100 there's some data. At address 101 there's more data, etc. If we have a list arr, that points to address 100, and arr[x] means "address 100+x`. Obviously, the details can get more complicated, especially with all the layers of abstraction before we get to Python, but that's the idea. Dictionaries and sets work on "hashes", meaning converting the item or key into a representative number. That number is converted into a list index and bob's your uncle, we're back at lists/arrays! Example Imagine a really bad hashing algorithm that just converts the first letter of a string into its position in the alphabet: a=0, b=1, c=2, etc. We could do arr = ['apple', 'banana', 'indigo'] - a standard list. If we use our really bad hashing algorithm and do {'apple': 53, 'indigo': 42} here's what happens. Initialize the array arr with size 5 'apple' is hashed to 0, so arr[0] = 53 'indigo' is hashed to 8. Our array is only size 5, so we mod divide. 8 % 5 = 3 so arr[3] = 42. Thus, arr ends up looking like [53, -, -, 42, -]. When we want the value associated with indigo, we do the same thing. indigo hashes to 8. 8 % 5 = 3 and arr[3] is 42. Obviously, Python does fancy things to remember the list of keys, expand the array as needed, and avoid "collisions" (e.g. dragonfruit would also have a key of 3). But that's the general idea.
🌐
Programiz
programiz.com › python-programming › list
Python Lists (With Code Visualization)
September 10, 2021 - Python lists store multiple data together in a single variable. In this tutorial, we will learn about Python lists (creating lists, changing list items, removing items, and other list operations) with the help of examples.