Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python append list and for statement

I want to get a list with variables and filter it

I succeeded in removing unnecessary words, but

filtered = [] does not work from here.
print(filtered) = []

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What I want is ‘USD’ or (and) ‘EUR’ according to the variable in filtered.

Append seems to be the problem… I don’t know the cause

And in the second code, why is the POA not deleted?
Can’t delete specific characters from list?


def test():
    response = [{'1': 0, '2': '2A', '3': True, '4': True, 'QA': ['NZDUSD','NZDEUR','NZDYEN','NZDGBP']}]
    if response == []:
        return

    else:
        response = str(response [0]['QA'])
        response = response.replace('NZD','')
        
        filtered = []
        for data in [response]:
            if data in ['USD','EUR']:
                filtered.append(data)
            # print(data)
        
        print(filtered)
        if 'USD' in filtered and 'EUR' in filtered:
            print ('USDEUR')
        elif 'USD' in filtered:
            print ('USD')
        elif 'EUR' in filtered:
            print ('EUR')

test()
def test():
    response = [{'1': 0, '2': '2A', '3': True, '4': True, 'QA': ['POAUSD','POAEUR','POAYEN','POAGBP']}]
    if response == []:
        return

    else:
        response = response [0]['QA']
        for i in range(len(response)):
            if response[i] == 'POA':
                response[i] = ''
        # print(response)
        filtered = []
        for data in [response]:
            if data in ['USD','EUR']:
                filtered.append(data)
            print(data)
        print(filtered)
        if 'USD' in filtered and 'EUR' in filtered:
            return print('USDEUR')
        elif 'USD' in filtered:
            return print('USD')
        elif 'EUR' in filtered:
            return print('EUR')

test()

>Solution :

About your 1st code: response is a string and not a list, so "for data in response" will yield individual characters, not the words you expect.

For it to work as you intend, modify this part of the code:

...
    else:
        response = response [0]['QA']
        response = [ word.replace('NZD','') for word in response]
        # print("result: ",response) # testing statement
        
        filtered = []
        for data in response:
...

About your 2nd code:

if response[i] == "POA":

will always fail; you want to replace it and the following line (amongst other solutions) with:

if response[i].startswith('POA'):
   response[i] = response[i][3:] # or ... = response[i].replace('POA','')

And further on,

for data in response:

instead of

for data in [response]:

(explanation: [response] is a list containing response as its unique value, so for data in [response]: is equivalent to "data = response")

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading