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

Function to get details of list of dictionaries

I created a list of dictionaries. Example:

my_list=[
  {
    "id": "first_host",
    "year": 2022,
    "hosts": ["50.60.70.80"]
  },
.....

I am trying to define a function to get these details of the list for each id.

def get_details(details, host_id):
     return config

How do we use it? My expectancy >

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

get_details(my_list, "first_host")

>Solution :

The expected return value of get_details() is unclear from the question.

I think this might be the one you are looking for, but not sure because of the ambiguity of the question.

my_list=[{"id": "first_host", "year": 2022, "size": "16g", "hosts": ["50.60.70.80"]},
         {"id": "second_host", "year": 2023, "size": "17g", "hosts": ["60.70.80.90"]},]

def get_details(details, host_id):
    for detail in details:
        if detail["id"] == host_id:
            return detail
get_details(my_list, "first_host")
> {'id': 'first_host', 'year': 2022, 'size': '16g', 'hosts': ['50.60.70.80']}
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