I am trying to return a list, I am getting different results when i return
def list_book():
lines = [['eofih', 'sdf', 'False'], ['sfdds', 'afds', 'False']]
books = [{"name": line[0], "author": line[1], "read": line[2]}
for line in lines
]
return books
This returns perfectly:
[{'name': 'eofih', 'author': 'sdf', 'read': 'False'},{'name': 'sfdds', 'author': 'afds', 'read': 'False'}]
But when i try doing as below:
def list_book():
lines = [['eofih', 'sdf', 'False'], ['sfdds', 'afds', 'False']]
for line in lines:
books = [{"name": line[0], "author": line[1], "read": line[2]}]
return books
This returns only the last entry in the list:
[{'name': 'sfdds', 'author': 'afds', 'read': 'False'}]
Please let me know is it possible to return the whole list in second option.
def list_book():
lines = [['eofih', 'sdf', 'False'], ['sfdds', 'afds', 'False']]
books = []
for line in lines:
books += [{"name": line[0], "author": line[1], "read": line[2]}]
return books
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.