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 str not list

I wanted to obtain array of int like [1,2,3]

but got [dict_values([1]),dict_values([2]),dict_values([3])]

taken_employee_ids_dict = [{‘id’: 1}, {‘id’: 2}, {‘id’: 3}]

my code

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

taken_employee_ids = [str(taken_employee_ids_list.values()) for taken_employee_ids_list in taken_employee_ids_dict]

in_values_check = ','.join(str(id) for id in taken_employee_ids)

And then in_values_check should go in:

qry = "SELECT CONCAT(first_name, ',', surname) AS full_name, id \
            FROM employees \
            WHERE date_terminated IS NULL \
            and id NOT IN " + "(" + in_values_check + ")" \
            " ORDER BY first_name, surname ASC"

sorry i am new to python thanks

>Solution :

You can do it with lambda or list comprehension

With Lambda,

taken_employee_ids_dict = [{'id': 1}, {'id': 2}, {'id': 3}]
in_values_check = list(map(lambda x : x['id'], taken_employee_ids_dict))
print(in_values_check)

With List Comprehension,

taken_employee_ids_dict = [{'id': 1}, {'id': 2}, {'id': 3}]
in_values_check = [dic['id'] for dic in taken_employee_ids_dict]
print(in_values_check)

Output:

[1, 2, 3]
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