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

Create list of previous lists in a dict values

I have a dictionary in which the values are lists. I need to create a list of these lists in the dictionary

My dict (which is the input)

input = [
    {
        'key': 'list1',
        'values': [4,5,2,3,4,5,2,3],
     }
    ,{
        'key': 'list2',
        'values': [1,1,34,12,40,3,9,7],
    }
]

The output I need:

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

[[4, 5, 2, 3, 4, 5, 2, 3], [1, 1, 34, 12, 40, 3, 9, 7]]

I’ve tried to write a code, but the output is wrong. The code I wrote:

lista_master = []

def compute_deviation(list_numbers):
    for dic in list_numbers:
      lista = list(dic.values())[1]
      lista_master.append(lista)
      print(lista_master)

The wrong output from my code:

[[4, 5, 2, 3, 4, 5, 2, 3]]
[[4, 5, 2, 3, 4, 5, 2, 3], [1, 1, 34, 12, 40, 3, 9, 7]]

What am I doing wrong and how can I fix it?

>Solution :

You are printing the value of lista_master every iteration of the loop, so a total 2 times in your case, you just need to move it outside the loop like so:

def compute_deviation(list_numbers):
    lista_master = [] # I also moved the "lista_master" inside the function
    for dic in list_numbers:
      lista = list(dic.values())[1]
      lista_master.append(lista)
    print(lista_master) #<--- here is the change 

now:

compute_deviation(input)

output:

[[4, 5, 2, 3, 4, 5, 2, 3], [1, 1, 34, 12, 40, 3, 9, 7]]
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