question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] Return lists

See original GitHub issue

Hi there!

First of all, gerat project! Thank You for sharing it!

i’m a somewhat a beginner, i’d like some help if you guys don’t mind. (I couldn’t find another forum for questions)

I’ve managed to read a list of objects inside a json, by passing the body like this:

[
  {
    "name": "Item name example 1",
    "price": 100,
    "is_offer": true
  },
  {
    "name": "Item name example 2",
    "price": 100,
    "is_offer": true
  },
  {
    "name": "Item name example 3",
    "price": 100,
    "is_offer": true
  }
]

The objective of my micro example is to receive a list of Items (the same base class of the API example), do some processing (change price value by a fixed factor) and return that altered list.

this works:

@app.put("/items/{item_id}")
def create_item(item_id: int, items: List[Item]):    
    return {"total" : len(items)}

and i can iterate trough the list with

for item in items:

but when i try to return the list with

return {items}

i get “TypeError: unhashable type: ‘list’” error.

is there a simple way to return the list? Or i’ll need to create the return string manually?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
tiangolocommented, Mar 2, 2019

Awesome @DaviOtero ! 🎉


As always, thanks @euri10 ! 👏 🌮 🍰

3reactions
euri10commented, Feb 25, 2019

You may (or I may misunderstand what you want to do) be mixing response_model and query parameters

  • to return a list you should have a response_model in the route @app.put("/items/{item_id}", response_model=List[Item])
  • the items: List[Item] part means you want a list of items in the query passed in the put
  • the curly braces around items most likely cast items to a set (not sure about that)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Top 25 Questions on Python List - Medium
25 questions on lists to enhance your knowledge on python lists. ... s[1:3] -Returns new list containing elements from index 1 till index ......
Read more >
Return a list from a function in Python - Stack Overflow
I'm new to python so pardon me if this is a silly question but I'm trying to return a list from a function....
Read more >
Solved Return a list containing all pairs of distinct | Chegg.com
Question : Return a list containing all pairs of distinct elements that satisfy the function fn. Note that the pair [1, 1] should...
Read more >
FAQ - The Florida Senate
Return to Question List. 4. How can I tell the difference between a House and a Senate bill? House and Senate bills are...
Read more >
Python return statement - GeeksforGeeks
A Python program to return multiple. # values from a method using list. # This function returns a list. def fun():. str =...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found