list value validate only from another list if they are exist in that list in python
You can validate a list of values by checking if each value exists in another list using a loop or list comprehension.
Here's an example using a loop:
list_a = [1, 2, 3, 4, 5]
list_b = [3, 5, 7, 9]
for value in list_b:
if value in list_a:
print(f"{value} exists in list_a")
else:
print(f"{value} does not exist in list_a")
Output:
3 exists in list_a
5 exists in list_a
7 does not exist in list_a
9 does not exist in list_a
Here's an example using a list comprehension:
list_a = [1, 2, 3, 4, 5]
list_b = [3, 5, 7, 9]
common_values = [value for value in list_b if value in list_a]
print(common_values)
Output:
[3, 5]
In this example, we create a new list common_values that contains only the values that exist in both list_a and list_b.
原文地址: https://www.cveoy.top/t/topic/bCoz 著作权归作者所有。请勿转载和采集!