1

I have a list of currency pairs, let's say for example it looks like this:

cp = ['EURUSD', 'CHFUSD', 'JPYUSD', 'CADUSD']

What I'm looking to do is iterate through this list, changing the USD to GBP to result in a new list that would display:

new_cp = ['EURGBP', 'CHFGBP', 'JPYGBP', 'CADGBP']

The way I assumed I would do it would be to loop through each pair, split the string into a list, remove the last 3 elements, and then append 'G', 'B', 'P' as the new last 3 elements, and finally returning this back in to a string, and adding it to the new list, 'new_cp'.

The code I began with was:

for pair in cp:
   split_pair = pair.split()

However, all this results in is getting:

['EURUSD']
['CHFUSD']

etc.

So it's just splitting the list, not splitting the string for each currency pair within the list.

I know this is relatively beginner stuff, but I am really stumped. I just don't get why this doesn't work.

If you can help with what I am doing wrong there, or even suggest a more efficient way to achieve what I'm looking to do that would be really appreciated.

3
  • 1
    How is .split supposed to know that you mean split into two sets of three characters? By default, it splits on any whitespace, of which there is none. Commented Feb 10, 2019 at 15:47
  • .split() without an argument, splits on whitespace, it won't magically identify you want the boundary at 'USD'/ after 3 chars- consider instead slicing the string or using .replace() Commented Feb 10, 2019 at 15:50
  • 1
    your data structure looks sub-optimal too, a list of tuples or a dict, would seem more natural e.g. [('EUR', 'USD'), ('CHF', 'USD')] etc. Commented Feb 10, 2019 at 15:54

4 Answers 4

5

I would suggest using the .replace() method

For example:

cp = ['EURUSD', 'CHFUSD', 'JPYUSD', 'CADUSD']
new_cp = []

for currency in cp:
    new_cp.append(currency.replace('USD', 'GBP'))

print(new_cp)

>> ['EURGBP', 'CHFGBP', 'JPYGBP', 'CADGBP']

Hope this helps :)

Sign up to request clarification or add additional context in comments.

1 Comment

one liner: [(currency.replace('USD', 'GBP') for currency in ['EURUSD', 'CHFUSD', 'JPYUSD', 'CADUSD']]
4

You could achieve this by using replace in a list comprehension:

cp = ['EURUSD', 'CHFUSD', 'JPYUSD', 'CADUSD']
new_cp = [word.replace('USD', 'GBP') for word in cp] 
#i.e.: for word in cp, we apply the specified function to it - replace the 'USD' in that word with 'GBP' - and then append it to a new list
print(new_cp)

outputs: ['EURGBP', 'CHFGBP', 'JPYGBP', 'CADGBP']

Comments

3

If you know that each currency pair will contain USD as the last three characters, the more efficient way is to just use list indexing and append GBP:

new_cp = [i[:-3] + 'GBP' for i in cp]

Comments

2

Here is another solution. You can use RegEx to replace the Currency values.

import re

cp = ['EURUSD', 'CHFUSD', 'JPYUSD', 'CADUSD']

mycurr = 'GBP'

to_curr = re.compile("USD")

for pair in cp:
    print(to_curr.sub(mycurr, pair))

Output:

EURGBP
CHFGBP
JPYGBP
CADGBP

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.