How to create a dictionary using python list comprehension from 2 list

Vanjith

This is my code:

a=[1,2,3,4,5,6,7]
b=[8,9,10,11,12,13,14]
c=[{i:j} for i in a for j in b]
print(c)

Output is

[{1: 8}, {1: 9}, {1: 10}, {1: 11}, {1: 12}, {1: 13}, {1: 14}, {2: 8}, {2: 9}, {2: 10}, {2: 11}, {2: 12}, {2: 13}, {2: 14}, {3: 8}, {3: 9}, {3: 10}, {3: 11}, {3: 12}, {3: 13}, {3: 14}, {4: 8}, {4: 9}, {4: 10}, {4: 11}, {4: 12}, {4: 13}, {4: 14}, {5: 8}, {5: 9}, {5: 10}, {5: 11}, {5: 12}, {5: 13}, {5: 14}, {6: 8}, {6: 9}, {6: 10}, {6: 11}, {6: 12}, {6: 13}, {6: 14}, {7: 8}, {7: 9}, {7: 10}, {7: 11}, {7: 12}, {7: 13}, {7: 14}]

But I want like:

[{1: 8, 2: 9, 3: 10, 4: 11, 5: 12, 6: 13, 7: 14}]

How to achieve this?

azro
  • To pair several list together you need to use zip operation zip(a, b),
  • You may use dict comprehension notation or dict constructor
  • Wrap in an array if needed
c = dict(zip(a, b))   # {1: 8, 2: 9, 3: 10, 4: 11, 5: 12, 6: 13, 7: 14}
c = [dict(zip(a, b))] # [{1: 8, 2: 9, 3: 10, 4: 11, 5: 12, 6: 13, 7: 14}]

c = {i: j for i, j in zip(a, b)}   # {1: 8, 2: 9, 3: 10, 4: 11, 5: 12, 6: 13, 7: 14}
c = [{i: j for i, j in zip(a, b)}] # [{1: 8, 2: 9, 3: 10, 4: 11, 5: 12, 6: 13, 7: 14}]

Your notation [{:} for _ in _] was creating a dict for each iteration, resulting in mulitple dicts in a list [{}, {}, {}]

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

Create a list of dictionaries from a global variable using map or comprehension list python

分類Dev

How to create a dictionary of dictionaries using dict comprehension?

分類Dev

Using list comprehension to search a 2d array (python)

分類Dev

Create and initialize dictionary from list

分類Dev

if else nested for loops using python list comprehension

分類Dev

Convert list to dictionary with duplicate keys using dict comprehension

分類Dev

how to make list comprehension using while in loop

分類Dev

How to calculate sum using list comprehension

分類Dev

How to add 0's into two dimensional list using list comprehension in python?

分類Dev

How to save repeated computation in list comprehension in Python?

分類Dev

python list comprehension double for

分類Dev

python list comprehension double for

分類Dev

Python List Comprehension With Equation

分類Dev

Chained list comprehension in python

分類Dev

Create a Dictionary of Dictionary from a Tuple of Dictionary Inside a List

分類Dev

How to use List comprehension to create strings of different sizes from specific characters

分類Dev

Get all documents from mongo collection using a nested list comprehension in Python

分類Dev

Get all documents from mongo collection using a nested list comprehension in Python

分類Dev

How can i create a list (new) from an existing list where items of list(new) will be list in python

分類Dev

dictionary-comprehension with conditional calling nested list-comprehension

分類Dev

Python list comprehension using a class variable throws NameError

分類Dev

Python - dynamic filter for list comprehension?

分類Dev

Python List Comprehension with two cycles

分類Dev

Python list comprehension Django objects

分類Dev

Unpacking "the rest" of the elements in list comprehension - python3to2

分類Dev

How do you print output and save in a file using list comprehension?

分類Dev

python one liner for creating dictionary from list

分類Dev

Get list from dictionary with list

分類Dev

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

Related 関連記事

  1. 1

    Create a list of dictionaries from a global variable using map or comprehension list python

  2. 2

    How to create a dictionary of dictionaries using dict comprehension?

  3. 3

    Using list comprehension to search a 2d array (python)

  4. 4

    Create and initialize dictionary from list

  5. 5

    if else nested for loops using python list comprehension

  6. 6

    Convert list to dictionary with duplicate keys using dict comprehension

  7. 7

    how to make list comprehension using while in loop

  8. 8

    How to calculate sum using list comprehension

  9. 9

    How to add 0's into two dimensional list using list comprehension in python?

  10. 10

    How to save repeated computation in list comprehension in Python?

  11. 11

    python list comprehension double for

  12. 12

    python list comprehension double for

  13. 13

    Python List Comprehension With Equation

  14. 14

    Chained list comprehension in python

  15. 15

    Create a Dictionary of Dictionary from a Tuple of Dictionary Inside a List

  16. 16

    How to use List comprehension to create strings of different sizes from specific characters

  17. 17

    Get all documents from mongo collection using a nested list comprehension in Python

  18. 18

    Get all documents from mongo collection using a nested list comprehension in Python

  19. 19

    How can i create a list (new) from an existing list where items of list(new) will be list in python

  20. 20

    dictionary-comprehension with conditional calling nested list-comprehension

  21. 21

    Python list comprehension using a class variable throws NameError

  22. 22

    Python - dynamic filter for list comprehension?

  23. 23

    Python List Comprehension with two cycles

  24. 24

    Python list comprehension Django objects

  25. 25

    Unpacking "the rest" of the elements in list comprehension - python3to2

  26. 26

    How do you print output and save in a file using list comprehension?

  27. 27

    python one liner for creating dictionary from list

  28. 28

    Get list from dictionary with list

  29. 29

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

ホットタグ

アーカイブ