Python Forum

Full Version: problem with print lists
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
>>> pprint(response.choices)
Output:
[Choice(finish_reason='length', index=0, message=ChatCompletionMessage(content='The time complexity of the code is O(n * k), where n is the value of parameter `n` and k is the value of parameter `k`. This is because there are two nested loops, one iterating `n` times and the other iterating `k` times. The accum variable is incremented `n *', role='assistant', function_call=None, tool_calls=None))]
i try print and pprint and response is as above ( other lists also the same)
How can i print this in readable format?
Please use code tags when posting code. This is hard to read.
Quote:Please use code tags when posting code. This is hard to read.
I believe that's the point. The part that is hard to read is the output of the pprint command.

The problem has nothing to do with lists. The list only contains a single item. The problem is that the item is not printing prettily. You could try printing the item instead of the list. That would call __str__ for the item instead of __repr__. This might result in prettier output.
for choice in response.choices:
    print(choice)
(Dec-14-2023, 03:04 PM)MarekGwozdz Wrote: [ -> ]How can i print this in readable format?
What exactly is considered "readable" format in this case (list of Choice object instance(s))?
How about some added newlines?

mystring = """[Choice(finish_reason='length', index=0, message=ChatCompletionMessage(content='The time complexity of the code is O(n * k), where n is the value of parameter `n` and k is the value of parameter `k`. This is because there are two nested loops, one iterating `n` times and the other iterating `k` times. The accum variable is incremented `n *', role='assistant', function_call=None, tool_calls=None))]"""
Then pprint (sounds like a stutter!)

pprint(mystring)
 
("[Choice(finish_reason='length', index=0, "
 "message=ChatCompletionMessage(content='The time complexity of the code is "
 'O(n * k), where n is the value of parameter `n` and k is the value of '
 'parameter `k`. This is because there are two nested loops, one iterating `n` '
 'times and the other iterating `k` times. The accum variable is incremented '
 "`n *', role='assistant', function_call=None, tool_calls=None))]")
Add some newlines:

newstring = mystring.replace(', ', ',\n').replace('. ', '.\n')
 
pprint(newstring)
 
("[Choice(finish_reason='length',\n"
 'index=0,\n'
 "message=ChatCompletionMessage(content='The time complexity of the code is "
 'O(n * k),\n'
 'where n is the value of parameter `n` and k is the value of parameter `k`.\n'
 'This is because there are two nested loops,\n'
 'one iterating `n` times and the other iterating `k` times.\n'
 "The accum variable is incremented `n *',\n"
 "role='assistant',\n"
 'function_call=None,\n'
 'tool_calls=None))]')
Readable??