How to get particular string from list in python

a=[('', 1), ('', 1), ('', 1)]

how to get value of api_key which is xyz from above mentioned a. please help me to write code in python.Thank you

2 answers

  • answered 2018-03-11 13:49 krishna Prasad

    Just looping over all elements and parsing url to get the api_key, have a look into below code:

    from urlparse import urlparse, parse_qs
    a=[('', 1), ('', 1), ('', 1)]
    for value in a:
        if len(value) > 1:
            url = value[0]
            if 'api_key' in parse_qs(urlparse(url).query).keys():
                print parse_qs(urlparse(url).query)['api_key'][0]



  • answered 2018-03-11 14:13 âńōŋŷXmoůŜ

    This will work too. Hope this helps.

    • Find all items that has the keyword 'api_key' (in url[0]),
    • Split it into columns, delimited by '=' (split by '=')
    • The last entry ([-1]) will be the answer (xyz).
    a=[('', 1), ('', 1), ('', 1)]
    for ky in [url for url in a if 'api_key' in url[0]]:
    Sample result: xyz