Is there a way to remove the list when the elements in the Python list are dictionaries?

Asked 2 years ago, Updated 2 years ago, 92 views

For example,

test_list_1 = [{key_1:val_1},{key_2:val_2},{key_3:val_3},{key_4:val_4}]
test_list_2 = [{key_3:val_3},{key_4:val_4},{key_5:val_5},{key_6:val_6}]

# Desired Value
# # test_list_2 - test_list_1 = [{key_5:val_5},{key_6:val_6}]

When there are two lists including dictionaries, Is there any way to subtract test_list_1 from test_list2?

python list dictionary

2022-09-21 20:34

1 Answers

If you use list compression as shown below, you can express it briefly.

[item for item in test_list_2 if not item in test_list_1]


2022-09-21 20:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.