Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
Dictionary in list

I want to print a dictionary inside a list like this :

[{name : 'red', id : '1'}, {name : 'yellow', id : '2'}, {name : 'black', id : '3'}, {name : 'white', id : '4'}]`

I don't want quotations in name and id. However, I want them in the values portion of that dictionary.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You'd have to write your own formatting function to do that.

Here's a hairy but terse function that does what you want:

def pretty_print(data):
    return '[%s]' % ', '.join(
        '{%s}' % ', '.join(
            '%s : %r' % (key, value) for key, value in item.items()
        ) for item in data
    )

So the following code:

print(pretty_print([
    {'name': 'red', 'id': '1'},
    {'name': 'yellow', 'id': '2'},
]))

Would print:

[{name : 'red', id : '1'}, {name : 'yellow', id : '2'}]
over 4 years ago · Santiago Trujillo Report

0

You can convert this like:

Code:

def fix_key_formatting(data_to_dump, keys_to_fix):
    return_str = json.dumps(data_to_dump)
    for key in keys_to_fix:
        return_str = return_str.replace('"%s":' % key, '%s:' % key)
    return return_str

Test Code:

data = [
    {'name': 'red', 'id': '1'},
    {'name': 'yellow', 'id': '2'},
    {'name': 'black', 'id': '3'},
    {'name': 'white', 'id': '4'}
]

import json
print(fix_key_formatting(data, ('id', 'name')))

Results:

[
    {name: "red", id: "1"}, 
    {name: "yellow", id: "2"}, 
    {name: "black", id: "3"}, 
    {name: "white", id: "4"}
]
over 4 years ago · Santiago Trujillo Report

0

You can create a simple class with a __repr__ method to store the string value:

class String:
   def __init__(self, val):
      self.val = val
   def __repr__(self):
      return self.val
data = [{'name' : 'red', 'id' : '1'}, {'name' : 'yellow', 'id' : '2'}, {'name' : 'black', 'id' : '3'}, {'name' : 'white', 'id' : '4'}]
final_data = [{String(c):d for c, d in i.items()} for i in data]

Output:

[{name: 'red', id: '1'}, {name: 'yellow', id: '2'}, {name: 'black', id: '3'}, {name: 'white', id: '4'}]

To access the string values, you can call the attribute:

string_values = [{c.val:d for c, d in i.items()} for i in final_data]

To apply the class String to all keys in an arbitrary structure, recursion can be used:

def convert_string(d):
   return {String(a):convert_string(b) if isinstance(b, dict) else b for a, b in d.items()}

data = [{'name' : {'first':'blue', 'last':'black', 'known_ids':[34, 5, 12, 34]}, 'id' : '1'}, {'name' : 'yellow', 'id' : '2'}, {'name' : 'black', 'id' : '3'}, {'name' : 'white', 'id' : '4'}]
new_data = list(map(convert_string, data))

Output:

[{name: {first: 'blue', last: 'black', known_ids: [34, 5, 12, 34]}, id: '1'}, {name: 'yellow', id: '2'}, {name: 'black', id: '3'}, {name: 'white', id: '4'}]
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!