How to convert a dictionary to a list of keys, with repeat counts given by the values?

Aarav Chandra

I need your help to solve a problem.

I want to convert a dictionary d = {key1:value1, key2:value2} into list= [keys1, keys1, ... (value1 times), keys2, ... (value2 times)] without using a nested loop.

Example:

d1 = {4: 1, 3: 2, 12: 2}

The code should produce the output:

l = [4, 3, 3, 12, 12]

This is what I have:

for key, value in nums1.items():
        temp = (str(key))*value
        nums2.append(int(temp))
print(nums2)

Which gives: [4, 33, 1212], but should give [4, 3, 3, 12, 12].

The complexity should be O(n).

Sven Marnach

The easiest solution is to use collections.Counter. It features an elements() method that yields all elements with the correct count:

>>> from collections import Counter
>>> list(Counter(d1).elements())
[4, 3, 3, 12, 12]

If you want to implement this yourself, I think the most readable version is this for loop:

from itertools import repeat

result = []
for k, count in d1.items():
    result += repeat(k, count)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to convert dictionary to dataframe when values of keys are list of list?

分類Dev

Assign list values to dictionary keys

分類Dev

Construct a dictionary from a list of dictionary keys, sub-keys and values

分類Dev

How to match keys in a dictionary with the elements in a list and store those key values to another dictionary in python?

分類Dev

How to convert output values into dictionary

分類Dev

How to add multiple values to dictionary keys in Python

分類Dev

How to sum up values of duplicate keys in a dictionary?

分類Dev

How to create three separate lists of values from a list of dictionaries where each dictionary has three keys

分類Dev

How to get dictionary keys and values if both keys are in two separated dictionaries?

分類Dev

Given a dictionary how can I check if two of its keys are the same?

分類Dev

Convert list to dictionary with duplicate keys using dict comprehension

分類Dev

Search dictionary values and return List of keys meeting conditions

分類Dev

Swap keys for values in dictionary?

分類Dev

How can I return common values of dictionary values for specific keys?

分類Dev

How to convert a list of dictionary values containing Timesamp objects into datetime objects in Python?

分類Dev

Convert JSON to map with string keys and List<String> values

分類Dev

How to list duplicate values in a dictionary that represents flights?

分類Dev

(More) nested dictionary comprehensions: How to switch inner values to keys?

分類Dev

How to write an excel file which has dictionary keys as column name and dictionary values as column values?

分類Dev

Assigning values to keys in a mutable dictionary

分類Dev

Match keys to values in dictionary in python

分類Dev

Converting pandas dataframe into dictionary where keys are index and values are list of column values

分類Dev

Compare keys of dictionary with values of another dictionary

分類Dev

How to update keys in dictionary?

分類Dev

How to make nested list from list where values are counts of list items Python

分類Dev

Inverting a dictionary with list values

分類Dev

Convert values from a dictionary to a matrix

分類Dev

Convert Dictionary Values into Array in Python

分類Dev

Python pandas: convert dictionary to DataFrame with keys as row

Related 関連記事

  1. 1

    How to convert dictionary to dataframe when values of keys are list of list?

  2. 2

    Assign list values to dictionary keys

  3. 3

    Construct a dictionary from a list of dictionary keys, sub-keys and values

  4. 4

    How to match keys in a dictionary with the elements in a list and store those key values to another dictionary in python?

  5. 5

    How to convert output values into dictionary

  6. 6

    How to add multiple values to dictionary keys in Python

  7. 7

    How to sum up values of duplicate keys in a dictionary?

  8. 8

    How to create three separate lists of values from a list of dictionaries where each dictionary has three keys

  9. 9

    How to get dictionary keys and values if both keys are in two separated dictionaries?

  10. 10

    Given a dictionary how can I check if two of its keys are the same?

  11. 11

    Convert list to dictionary with duplicate keys using dict comprehension

  12. 12

    Search dictionary values and return List of keys meeting conditions

  13. 13

    Swap keys for values in dictionary?

  14. 14

    How can I return common values of dictionary values for specific keys?

  15. 15

    How to convert a list of dictionary values containing Timesamp objects into datetime objects in Python?

  16. 16

    Convert JSON to map with string keys and List<String> values

  17. 17

    How to list duplicate values in a dictionary that represents flights?

  18. 18

    (More) nested dictionary comprehensions: How to switch inner values to keys?

  19. 19

    How to write an excel file which has dictionary keys as column name and dictionary values as column values?

  20. 20

    Assigning values to keys in a mutable dictionary

  21. 21

    Match keys to values in dictionary in python

  22. 22

    Converting pandas dataframe into dictionary where keys are index and values are list of column values

  23. 23

    Compare keys of dictionary with values of another dictionary

  24. 24

    How to update keys in dictionary?

  25. 25

    How to make nested list from list where values are counts of list items Python

  26. 26

    Inverting a dictionary with list values

  27. 27

    Convert values from a dictionary to a matrix

  28. 28

    Convert Dictionary Values into Array in Python

  29. 29

    Python pandas: convert dictionary to DataFrame with keys as row

ホットタグ

アーカイブ