0

I'm coming from PHP to python.

In PHP i do this:

$result = array();

foreach($videos as $video) {
  $result[] = array("title"=>$video->title,"description"=>$video->title);
}

print_r($result);

Now when i try this in python flask i get an error We're sorry, but something went wrong: Web application could not be started

result = {}

for video in videos:
    title = video['title']
    description = video['description']
    content = {'title':title, 'description': description}
    result[] = content

How do i solve?

Here is full function

@app.route("/add_channel", methods=['POST'])
def add_channel():
    if request.method == "POST":
        id=request.form['channel']
        videos = get_channel_videos(id)

         result = []
         for video in videos:
             result.append({'id': video['id'], 'title': video['snippet']['title']})
    return result
0

1 Answer 1

1

If your final result is a list of dictionaries, that last line would be

result.append(content)

More concisely that could be achieved in a list comprehension

result = [{'title': video['title'], 'description': video['description']} for video in videos]
Sign up to request clarification or add additional context in comments.

7 Comments

tried result.append(content) and get this error AttributeError: 'dict' object has no attribute 'append'
just change result = {} to result = [], result = {} declares a dictionary
You would use list initialization before the loop result = [] otherwise {} creates a dictionary
Still get an error ** TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a list.**
I've tried all suggestions but still get an error when i return the result.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.