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

Retrieve value from dictionary with list

I see so many questions but not found any with dictionary like that:

I have this dict:

my_dict = {
    'og_min': 'https://www.example.com/assets/images/empreendimentos/listing-varanda-vila-ema.png?_=1000',
    'images_sobre_o_produto': ['https://www.example.com/assets/images/enterprises/varanda-vila-ema/fachada.png?_=1000', 'https://www.example.com/assets/images/enterprises/retrato-by-dialogo/living.png?_=1000']}

How can iterate over that dict to get all urls one at time?

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

If I use something like:

for values in my_dict.values():
    print(values)
    for value in values:
        print(value)

The first value from og_min key is split, how to avoid that?

>Solution :

The fact there is you have a dictionary, with two keys.
One key is a string, and the other holds a list.

So you can do something like this:

for key, val in my_dict.items():
    if type(val) is list:
        for url in val:
            print(url)
    if type(val) is str:
        print(val)

That will give you the url’s output you are looking for.

Of course this code is assuming that you have to type of values, a string and a list type, and the string value holds a url and the list type contains a list of url’s.

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