Shivpe_R Shivpe_R - 6 days ago 4
Python Question

Check For String/Set of Strings(Separated by |) available in List of strings Python

I have a list of String like

Countries = ['United Kingdom', 'United States', 'Luxembourg', 'Canada', 'China', 'Brazil', 'Hong Kong', 'Indonesia', 'Japan', 'India', 'Ireland', 'Germany', 'Australia', 'France', 'Switzerland', 'Malta', 'South Korea', 'Russia', 'Taiwan', 'Bermuda', 'Turkey', 'Mexico', 'Not Applicable', 'Argentina', 'South Africa', 'Malaysia', 'Thailand', 'Singapore', 'Qatar', 'Italy']

str1 = 'United Kingdom'
str2 = 'United States|China|Hong Kong|Italy'

len([col for col in Countries if str1 in col]) > 0
returns TRUE


How can i check whether all countries in str2 are available in the
Countries
list?

Answer

You can use the all statement to check if every country in the str2 is in Countries:

Countries = ['United Kingdom', 'United States', 'Luxembourg', 'Canada', 'China', 'Brazil', 'Hong Kong', 'Indonesia', 'Japan', 'India', 'Ireland', 'Germany', 'Australia', 'France', 'Switzerland', 'Malta', 'South Korea', 'Russia', 'Taiwan', 'Bermuda', 'Turkey', 'Mexico', 'Not Applicable', 'Argentina', 'South Africa', 'Malaysia', 'Thailand', 'Singapore', 'Qatar', 'Italy']

str1 = 'United Kingdom'
str2 = 'United States|China|Hong Kong|Italy'

str2 = str2.split("|")

if all(i in Countries for i in str2):

   #do something