Python List of Dictionaries by Loops

2024/11/23 19:50:04

I have 2 python list of dictionaries:

[{'index':'1','color':'red'},{'index':'2','color':'blue'},{'index':'3','color':'green'}
]

and

[{'device':'1','name':'x'},{'device':'2','name':'y'},{'device':'3','name':'z'}
]

How can I append each dictionary from the second list to the first list so as to get an output as:

[{'device':'1','name':'x','index': '1', 'color': 'red'},{'device':'1','name':'x','index': '2', 'color': 'blue'},{'device':'1','name': 'x','index': '3', 'color': 'green'}{'device':'2','name':'y''index': '1', 'color': 'red'},{'device':'2','name':'y','index': '2', 'color': 'blue'},{'device':'2','name':'y','index': '3', 'color': 'green'}{'device':'3','name':'z','index': '1', 'color': 'red'}, {'device':'3','name':'z','index': '2', 'color': 'blue'}, {'device':'3','name':'z','index': '3', 'color': 'green'}
]
Answer

If you want only print the resulting dictionaries, uncomment the print statement (and comment the following 2).

d1 = [{'index':'1','color':'red'},{'index':'2','color':'blue'},{'index':'3','color':'green'}
]d2 = [{'device':'1','name':'x'},{'device':'2','name':'y'},{'device':'3','name':'z'}
]result_list = []
for dict1 in d1:merged_dict = dict1.copy()for dict2 in d2:merged_dict.update(dict2)
#       print(merged_dict)result_list.append(merged_dict.copy())print(result_list)

The result:

[{'name': 'x', 'device': '1', 'color': 'red', 'index': '1'},
{'name': 'y', 'device': '2', 'color': 'red', 'index': '1'},
{'name': 'z', 'device': '3', 'color': 'red', 'index': '1'},
{'name': 'x', 'device': '1', 'color': 'blue', 'index': '2'},
{'name': 'y', 'device': '2', 'color': 'blue', 'index': '2'},
{'name': 'z', 'device': '3', 'color': 'blue', 'index': '2'},
{'name': 'x', 'device': '1', 'color': 'green', 'index': '3'},
{'name': 'y', 'device': '2', 'color': 'green', 'index': '3'},
{'name': 'z', 'device': '3', 'color': 'green', 'index': '3'}]

https://en.xdnf.cn/q/120616.html

Related Q&A

Removing parentheses and comma

Im importing Data from a database into python data frame. Now, I wish to use the data for further analysis, however, I need to do a little cleaning of the data before using. Currently, the required col…

Explicit Exception problem with try function

I have to run codes irrespective whether it fails or not. Im using ExplicitException. Following is my code:try:G.add_edge(data1[0][0],data1[1][0],weight=data1[2+i][0]) except ExplicitException:passtry:…

How do I perform a bubble sort in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.Improve…

check if number is between row of numpy array

Want to check if value is between the row of array. here the value 347 is in between the 1st row of aa but not second , so how to check for it ? aa= np.array([[348 ,345],[460 , 459 ]])value = 347prin…

Print a word diagonally? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Click a button using Selenium and Python

I have the following code: <a class="sectionname" href="#" onclick="expandAll();return false;">Expand all</a> When I click on expand all, the whole page loads…

how to do Json format [duplicate]

This question already has answers here:How to store ner result in json/ database(2 answers)Closed 8 years ago.(S(PERSON Rami/NNP Eid/NNP)is/VBZstudying/VBGat/IN(ORGANIZATION Stony/NNP Brook/NNP Univers…

python numpy arange dtpye? why converting to integer was zero

x = np.arange(0.3, 12.5, 0.6)print(x)[ 0.3 0.9 1.5 2.1 2.7 3.3 3.9 4.5 5.1 5.7 6.3 6.9 7.5 8.1 8.7 9.3 9.9 10.5 11.1 11.7 12.3]x = np.arange(0.3, 12.5, 0.6,int)print…

Where is my syntax error?

Im trying to see where a Python syntax error would be hiding. Both Django and pylint claim a syntax error at custom.py:41. Lines 41-42 read:(reading_threshold =int(request.POST[reading_threshold]))I do…

programming challenge: how does this algorithm (tied to Number Theory) work?

In order to work on my python skills, I am sometimes doing various challenges on the internet (eg on hackerrank). Googling for something else, I found this problem, and the accompanying solution on the…