Brick Top
Brick Top

Reputation: 85

Python - Check if word matches list of words with IF statement

I'm trying to check with an IF if a particular string matches a few words at the same time. Put in code, it would be like this:

if shippingDest == "Spain":
    shippingRegion = "Spain"
if shippingDest == "Portugal":
    shippingRegion = "Portugal"
if shippingDest == ["France", "Germany", "Luxembourg", "Belgium", "United Kingdom"]:
    shippingRegion = "Euro1"
if shippingDest == ["Albania", "Andorra", "Austria", "Belarus", "Bosnia and Herzegovina", "Bulgaria", "Croatia", "Czech Republic", "Denmark", "Estonia", "Faroe Islands", "Finland", "Georgia", "Gibraltar", "Greece", "Hongary", "Iceland", "Ireland", "Italy", "Latvia", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Malta", "Moldova", "Monaco", "Montenegro", "Netherlands", "Norway", "Poland", "Romania", "Russia", "San Marino", "Serbia", "Slovenia", "Svalbard and Jan Mayen", "Sweden", "Switzerland", "Ukraine", "Vatican City State"]:
    shippingRegion = "Euro2"
else:
    shippingRegion = "World"

When checking this with, for example, Germany as the country, shippingRegion would be Euro1. However, it takes it as World.

Can anyone point me in the right direction to see if a word matches any of a set of words with an IF?

Thanks,

Upvotes: 0

Views: 7325

Answers (7)

Qantas 94 Heavy
Qantas 94 Heavy

Reputation: 16020

Use the in operator:

if shippingDest == "Spain":
    shippingRegion = "Spain"
elif shippingDest == "Portugal":
    shippingRegion = "Portugal"
elif shippingDest in ["France", "Germany", "Luxembourg", "Belgium", "United Kingdom"]:
    shippingRegion = "Euro1"
elif shippingDest in ["Albania", "Andorra", "Austria", "Belarus", "Bosnia and Herzegovina", "Bulgaria", "Croatia", "Czech Republic", "Denmark", "Estonia", "Faroe Islands", "Finland", "Georgia", "Gibraltar", "Greece", "Hongary", "Iceland", "Ireland", "Italy", "Latvia", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Malta", "Moldova", "Monaco", "Montenegro", "Netherlands", "Norway", "Poland", "Romania", "Russia", "San Marino", "Serbia", "Slovenia", "Svalbard and Jan Mayen", "Sweden", "Switzerland", "Ukraine", "Vatican City State"]:
    shippingRegion = "Euro2"
else:
    shippingRegion = "World"

When applied to a list, in operator checks for whether or not a value is contained in the list.

It might be better to use a set instead, as your values are not ordered. The set syntax is as follows:

{'something', 'something else', 'another thing', '...'}

It's similar to a list, except this data structure is not ordered. Like a list, you can also use in to check if a particular value is contained within a set.

Note: it seems you want elif, not if - using if makes any country that isn't in the Euro2 zone become the World region instead, including Spain and Portugal.

Upvotes: 2

Steve P.
Steve P.

Reputation: 14699

if shippingDest == ["France", "Germany", "Luxembourg", "Belgium", "United Kingdom"]:
    shippingRegion = "Euro1"

This does not see if shippingDest is one of those strings, instead it checks if shippingDest is equal to that list.

Instead, you could use:

if shippingDest in ["France", "Germany", "Luxembourg", "Belgium", "United Kingdom"]:
    shippingRegion = "Euro1"

Since the preceding conditionals evaluated to false, the else executed. Additionally, for mutually exclusive conditions, you want to use elif instead of a series of if statements followed by one else. In your code, that else only corresponds to the preceding if.

Upvotes: 2

rlms
rlms

Reputation: 11050

Replace == with in:

if shippingDest in ["France", "Germany", "Luxembourg", "Belgium", "United Kingdom"]:

Your code would also be more efficient if you replaced the if statements after the first with elifs, meaning else if.

Also, you could shorten it like this:

if shippingDest in ("France", "Germany", "Luxembourg", "Belgium", "United Kingdom"):
    shippingRegion = "Euro1"
elif shippingDest in ("Albania", "Andorra", "Austria", "Belarus", "Bosnia and Herzegovina", "Bulgaria", "Croatia", "Czech Republic", "Denmark", "Estonia", "Faroe Islands", "Finland", "Georgia", "Gibraltar", "Greece", "Hongary", "Iceland", "Ireland", "Italy", "Latvia", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Malta", "Moldova", "Monaco", "Montenegro", "Netherlands", "Norway", "Poland", "Romania", "Russia", "San Marino", "Serbia", "Slovenia", "Svalbard and Jan Mayen", "Sweden", "Switzerland", "Ukraine", "Vatican City State"):
    shippingRegion = "Euro2"
elif shippingDest in ("Portugal", "Spain"):
    shippingRegion = shippingDest:
else:
    shippingRegion = "World"

I have also turned your lists into tuples (replaced [] with ()) because you don't need to change them later, so making them immutable means less memory is used.

Upvotes: 4

Jon Clements
Jon Clements

Reputation: 142106

You need to use elif's and in to check for membership instead of ==

if shippingDest == "Spain":
    shippingRegion = "Spain"
elif shippingDest == "Portugal":
    shippingRegion = "Portugal"
elif shippingDest in ["France", "Germany", "Luxembourg", "Belgium", "United Kingdom"]:
    shippingRegion = "Euro1"
elif shippingDest in ["Albania", "Andorra", "Austria", "Belarus", "Bosnia and Herzegovina", "Bulgaria", "Croatia", "Czech Republic", "Denmark", "Estonia", "Faroe Islands", "Finland", "Georgia", "Gibraltar", "Greece", "Hongary", "Iceland", "Ireland", "Italy", "Latvia", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Malta", "Moldova", "Monaco", "Montenegro", "Netherlands", "Norway", "Poland", "Romania", "Russia", "San Marino", "Serbia", "Slovenia", "Svalbard and Jan Mayen", "Sweden", "Switzerland", "Ukraine", "Vatican City State"]:
    shippingRegion = "Euro2"
else:
    shippingRegion = "World"

What's happening is that shippingDest is being set correctly, then failing the last if, so the else clause is executed, resulting in World.

It'd make more sense, and be more maintainable if you had a lookup table, using a dict:

shipping_destinations = {
    'Portugal': 'Portugal',
    'France': 'Euro1',
    'Monaco': 'Euro2',
    'spain': 'Spain'
}

Then, once those were listed out, you could do:

ship_to = shipping_destinations.get(country_name, 'World')

To retrieve the destination or default it to world. This saves if statements and if you had a spreadsheet of zones, would be easy to populate from that...

Upvotes: 2

Yaron
Yaron

Reputation: 233

You can use IN

if shippingDest in ["France", "Germany", "Luxembourg", "Belgium", "United Kingdom"]:

Upvotes: 1

Faruk Sahin
Faruk Sahin

Reputation: 8726

Try in :

if shippingDest in ["France", "Germany", "Luxembourg", "Belgium", "United Kingdom"]:
    shippingRegion = "Euro1"

Upvotes: 2

Vikram Saran
Vikram Saran

Reputation: 1143

You want to use the keyword elif - which is analogous to other languages else if.

Additionally, you want to test if the string ("Germany") is IN the list, use the in keyword instead of ==.

Upvotes: 2

Related Questions