Correct list comprehension is [[(k,v) for k,v in d.items()] for d in ld]

Demo:

>>> ld = [{'a': 10, 'b': 20}, {'p': 10, 'u': 100}]
>>> [[(k,v) for k,v in d.items()] for d in ld]
[[('a', 10), ('b', 20)], [('p', 10), ('u', 100)]]
>>> [[(k,v) for k,v in d.items() if k not in ['b','u']] for d in ld]
[[('a', 10)], [('p', 10)]]
Answer from Irshad Bhat on Stack Overflow
Discussions

Python list comprehension for dictionaries in dictionaries? - Stack Overflow
I just learned about list comprehension, which is a great fast way to get data in a single line of code. But something's bugging me. In my test I have this kind of dictionaries inside the list: [... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python Dictionary comprehension for a list of dictionaries - Stack Overflow
Oh my bad - so used to Python 3. I must say though, pretty new here and that's the fastest set of downvotes I've gotten - is it because of the lack of Python 2 support or is there something else? 2017-01-25T11:20:53.537Z+00:00 ... @tehjoker yes, well, dictionary comprehensions only work for ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
January 25, 2017
Python dictionary comprehension on a list of dictionaries - Stack Overflow
I have a list of dictionaries. Each dictionary has a large number of keys. I want to just keep certain keys that I already know, let's say keys 'a', 'b', 'c'. Each dict will have these keys. I know... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Python3 Dictionary Comprehension - Stack Overflow
I'm having difficulty working out a dictionary comprehension. I have a list of dictionaries, where each dictionary contains identical keys with different values: list_of_dictionaries = [{k1:v1, k... More on stackoverflow.com
๐ŸŒ stackoverflow.com
December 13, 2020
Top answer
1 of 3
2

Can't claim it's more "concise", but it's a dict comprehension in the end :D

from itertools import groupby
from operator import itemgetter
sort_list = sorted(file_list, key=itemgetter('size'))
groups = groupby(sort_list, key=itemgetter('size'))
print({k:[i['path'] for i in g] for k, g in groups})
2 of 3
2

If, for some reason, you want to avoid the import of defaultdict, this is one way to do it:

import json

file_list = [
    {'path': '/path/to/file1', 'size': 100, 'time': '2022-02-15'},
    {'path': '/path/to/file2', 'size': 200, 'time': '2022-02-13'},
    {'path': '/path/to/file3', 'size': 300, 'time': '2022-02-12'},
    {'path': '/path/to/file4', 'size': 200, 'time': '2022-02-11'},
    {'path': '/path/to/file5', 'size': 100, 'time': '2022-02-1-'}]

new_dict = {}

for file in file_list:
    new_dict[file['size']] = (files := new_dict.get(file['size'], []))
    files.append(file['path'])

print(json.dumps(new_dict, indent=4, sort_keys=True))

And if you don't like the constant reassignment of that list to the dicrionary entry (I don't really):

for file in file_list:
    if file['size'] not in new_dict:
        new_dict[file['size']] = []
    new_dict[file['size']].append(file['path'])

(Note that the default indent depth for Python is 4, you would do others and your future self a favour by adopting that sooner rather than later)

As indicated in the comments, a perhaps more elegant solution:

for file in file_list:
    new_dict.setdefault(file['size'], []).append(file['path'])

However, although it's possible to come up with a single line comprehension, it won't be more efficient, faster, or readable. In fact, likely none of those - so what would be the point?

Shorter code is often better code if it doesn't compromise on function or readability, but should never be a goal by itself.

For example, consider this (bad) example:

c_dict = {size: [fp['path'] for fp in file_list if fp['size'] == size] for size in set(fs['size'] for fs in file_list)}

And although that's a single line, you'd probably want to write it like this for readability, at which point you just have more code than before:

c_dict = {
    size: [fp['path'] for fp in file_list if fp['size'] == size] 
    for size in set(fs['size'] for fs in file_list)
}
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-dictionary-comprehension
Python Dictionary Comprehension - GeeksforGeeks
April 18, 2026 - The fromkeys() method creates a dictionary by taking a group of keys and assigning the same value to all of them. ... Example 1: This example demonstrates creating a dictionary by transforming and repeating characters from a string.
Find elsewhere
Top answer
1 of 4
3

Note how each line only differs by the key used. This means that the keys should be iterated over:

list_of_dictionaries  = [{1: 2, 2: 3}, {1: 4, 2: 5}, {1: 6, 2: 7}]

# Only safe if you know there will always be at least one dictionary
keys = list_of_dictionaries[0].keys()  

dictionary_of_lists = \
     {k: [i[k] for i in list_of_dictionaries]
      for k in keys}  # A second level of iteration to automate what you were doing manually before

print(dictionary_of_lists)
>>> {1: [2, 4, 6], 2: [3, 5, 7]}
2 of 4
1

If you are allowed to use pandas, this is a much simpler solution.

Using pandas, here's what you will get:

import pandas as pd
list_of_dicts = [{'k1':'v1', 'k2':'v2'}, {'k1':'v3', 'k2':'v4'},
                 {'k1':'v5', 'k2':'v6'}, {'k1':'v7', 'k2':'v8'},
                 {'k1':'v9', 'k2':'v10'}]
df = pd.DataFrame(list_of_dicts)
k = {c:df[c].tolist() for c in df.columns}
print (k)

The output of this will be:

{'k1': ['v1', 'v3', 'v5', 'v7', 'v9'], 'k2': ['v2', 'v4', 'v6', 'v8', 'v10']}

With this approach, you can keep adding as many keys as you like, the solution will be same.

import pandas as pd
list_of_dicts = [{'k1':'v1' , 'k2':'v2' , 'k3': 'v3'},
                 {'k1':'v4' , 'k2':'v5' , 'k3': 'v6'},
                 {'k1':'v7' , 'k2':'v8' , 'k3': 'v9'},
                 {'k1':'v10', 'k2':'v11', 'k3': 'v12'},
                 {'k1':'v13' ,'k2':'v14', 'k3': 'v15'}]
df = pd.DataFrame(list_of_dicts)
k = {c:df[c].tolist() for c in df.columns}
print (k)

This will result in:

{'k1': ['v1', 'v4', 'v7', 'v10', 'v13'], 'k2': ['v2', 'v5', 'v8', 'v11', 'v14'], 'k3': ['v3', 'v6', 'v9', 'v12', 'v15']}

The only limitation is that each set of dicts have to have same number of elements (k1, k2, k3). You cannot have (k1,k2) and (k1,k2,k3). Then the code will break as the dataframe is looking for same number of elements per column.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-convert-list-of-dictionaries-to-dictionary-value-list
Python - Convert list of dictionaries to Dictionary Value list - GeeksforGeeks
July 15, 2025 - For example, given a list of ... of dictionaries into a dictionary of value lists by iterating over the keys of the first dictionary to define the keys of the result....
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ artificial intelligence โ€บ 10 examples to master python dictionary comprehensions
10 Examples to Master Python Dictionary Comprehensions | Towards Data Science
January 2, 2021 - ... We can use the items of an existing dictionary as iterable in a dictionary comprehension. It allows us to create dictionaries based on existing dictionaries and modify both keys and values.
๐ŸŒ
Real Python
realpython.com โ€บ python-dictionary-comprehension
Python Dictionary Comprehensions: How and When to Use Them โ€“ Real Python
November 13, 2024 - Understanding dictionary comprehensions is crucial for you as a Python developer because theyโ€™re a Pythonic tool for dictionary manipulation and can be a valuable addition to your programming toolkit. ... To get the most out of this tutorial, you should be familiar with basic Python concepts, such as for loops, iterables, and dictionaries, as well as list comprehensions.
๐ŸŒ
Dataquest
dataquest.io โ€บ home โ€บ blog โ€บ python dictionary comprehension tutorial (with 39 code examples)
Python Dictionary Comprehension Tutorial (with 39 Examples)
March 6, 2023 - Values come second, so the index should be 1, while for keys it would be 0. Keys are usually assigned to lambda functions which are anonymous functions often used in Python. Do you think it is possible to apply a for loop to achieve the same result? Sometimes we face a list of dictionaries, and we want one single dictionary. It can also be done with dictionary comprehension.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-convert-list-of-dictionaries-to-dictionary-of-lists
Convert List of Dictionaries to Dictionary of Lists - Python - GeeksforGeeks
July 23, 2025 - This method uses dictionary comprehension to convert a list of dictionaries into a dictionary where each key maps to a list of values from the original dictionaries. The values for each key are collected across all dictionaries in the list.
๐ŸŒ
DEV Community
dev.to โ€บ michaelcurrin โ€บ dict-comprehensions-in-python-3hl7
Dictionary comprehensions in Python - DEV Community
November 7, 2020 - You can use an if statement in a dictionary comprehension. my_dict = {'A': 1, 'B': 2, 'C': 3} filtered_dict = {k: v for k, v in my_dict.items() if v < 3} filtered_dict # {'A': 1, 'B': 2} ... my_dict = {'A': 1, 'B': 2, 'C': 3} inverse = {v: k ...
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ dictionary-comprehension-python
Python Dictionary Comprehension with Examples | FavTutor
July 3, 2026 - Learn Python dictionary comprehension in one line: build a dict from a list, two lists with zip, filter with if, and swap keys and values.