python - Returning multiple variables from a single list -


using python 3

this basic i'm sure. code used return country, country code provided. need first 2 letters of input given.

the code i've worked far output first "country code"

def get_country_codes(prices):     c = prices.split(',')     char in c:         return char[:2]   print(get_country_codes("nz$300, kr$1200, dk$5"))  output:    nz wanted output:    nz, kr, dk 

def get_country_codes(prices):     values = []     price_codes = prices.split(',')     price_code in price_codes:          values.append(price_code.strip()[0:2])     return values  print(get_country_codes("nz$300, kr$1200, dk$5"))  

basically method returning first value split list.

you need iterate on split list , save each value in list , return that.

another approach:

country_price_values = "nz$300, kr$1200, dk$5"  country_codes = [val.strip()[0:2] val in country_price_values.split(',')] 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -