Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ def read_json_field(filename):
data = json.load(file)
output = []
for item in data:
instruction = item["instruction"]
output = item["output"]
output.append({"prompt": instruction, "chosen": output})
output.append({"prompt": item["instruction"], "chosen": item["output"]})
Comment on lines 33 to +35

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using a list comprehension is more idiomatic, concise, and efficient in Python compared to initializing an empty list and appending to it in a loop.

Suggested change
output = []
for item in data:
instruction = item["instruction"]
output = item["output"]
output.append({"prompt": instruction, "chosen": output})
output.append({"prompt": item["instruction"], "chosen": item["output"]})
output = [
{"prompt": item["instruction"], "chosen": item["output"]}
for item in data
]

return output
except FileNotFoundError:
logging.error("The file was not found.")
Expand Down