Skip to content Skip to sidebar Skip to footer

Convert List Of Dictionaries To Comma Separated String Python

i'm trying to convert list of dictionaries to comma separated string , but some extra fields of dictionary are coming data = [{'groupid': '28', 'name': 'TEST 2', 'internal': '0', '

Solution 1:

Try this:

data = [{'groupid': '28', 'name': 'TEST 2', 'internal': '0', 'flags': '0'}, {'groupid':'27', 'name': 'CUSTOMER/TEST 1', 'internal': '0', 'flags': '0'}]
print([d["name"] for d in data])

Output:

['TEST 2', 'CUSTOMER/TEST 1']

Solution 2:

For the expected out, you simply need to get the value corresponding to the key name:

s = ','.join(i['name'] for i in data)

Solution 3:

Data is defined as dictionary object. Apply elt for filter.


data = [{'groupid': '28', 'name': 'TEST 2', 'internal': '0', 'flags': '0'}, 
      {'groupid':'27', 'name': 'CUSTOMER/TEST 1', 'internal': '0', 'flags': '0'}]

expected output = [elt["name"] for elt in data]
print(expected output)
output = ['TEST 2', 'CUSTOMER/TEST 1']

Post a Comment for "Convert List Of Dictionaries To Comma Separated String Python"