-3

It returns None. How to assign to video_list the list values?

video_list = [print(video.title) for video in videos]

EDIT : The process to get videos list is very long , that's why I asked for

ghost239
  • 121
  • 7
  • Drop the `print`. You want to store a reference to `video.title`, not print its value to standard output. – chepner Jan 17 '23 at 13:45
  • Assign a list without the `print` and just `print` it afterwards... – Tomerikoo Jan 17 '23 at 13:46
  • 1
    Note: It is considered poor form to use a comprehension for side-effects: https://stackoverflow.com/questions/5753597/is-it-pythonic-to-use-list-comprehensions-for-just-side-effects – JonSG Jan 17 '23 at 13:52

4 Answers4

0

You should not print. Just get the value video.title . So answer video_list = [video.title for video in videos]

0
video_list = [video.title for video in videos]

You can't use print in a list comprehension. Now you will have all values in video_list and you can print it as a list to see the values print(video_list)

Ikram Khan Niazi
  • 789
  • 6
  • 17
0

Since print returns None, you could use or to return the value after printing, but this is not recommended.

video_list = [print(video.title) or video.title for video in videos]

It is better to first create the list, then print it after.

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

It would be more readable to keep assigning and printing apart:

video_list = [video.title for video in videos]
print(*video_list, sep='\n')
bereal
  • 32,519
  • 6
  • 58
  • 104