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.
.splitsupposed to know that you mean split into two sets of three characters? By default, it splits on any whitespace, of which there is none..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()[('EUR', 'USD'), ('CHF', 'USD')]etc.