-1

I have a tuple of tuples:

COUNTRIES = (
    ('AFG', 'Afghanistan'),
    ('ALA', 'Aland Islands'),
    ('ALB', 'Albania'),
    ('DZA', 'Algeria'),
    ('ASM', 'American Samoa')
)

I tried:

if country in COUNTRIES[0] and didn't worked - I understand why;

Besides looping over the main tuple and check one by one, there is something similar to 'in' ?

user3541631
  • 3,686
  • 8
  • 48
  • 115
  • 4
    because Python is case-sensitive. `COUNTRIES` is not the same as `Countries`. What error did you get? – Ma0 May 15 '18 at 13:55
  • What is your question? I do not understand the last sentence. – Ma0 May 15 '18 at 13:57
  • @Ev. Kounis , thanks, was just a typing mistake; I have a country str that correspond to the first element of the each tuple; my question is beside using for and going thru all of then, and inside the loop using if, are any other more pythonic solutions – user3541631 May 15 '18 at 14:17

2 Answers2

3

I would use chain in this case.

from itertools import chain

COUNTRIES = (
    ('AFG', 'Afghanistan'),
    ('ALA', 'Aland Islands'),
    ('ALB', 'Albania'),
    ('DZA', 'Algeria'),
    ('ASM', 'American Samoa')
)

if 'Albania' in chain.from_iterable(COUNTRIES):
    # do something
BcK
  • 2,548
  • 1
  • 13
  • 27
2

It looks like you might want to use a dict. What about this?

COUNTRIES = {
    'AFG': 'Afghanistan',
    'ALA': 'Aland Islands',
    'ALB': 'Albania',
    'DZA': 'Algeria',
    'ASM': 'American Samoa'
}

country = "AFG"
if country in COUNTRIES:
    print "I found " + COUNTRIES[country]
Mattias Backman
  • 927
  • 12
  • 25