I made this chunk of code but I think this isn't the "best" out there... how can I create a list made of other variables?
track_data = []
track_data.append(track['title'][0])
track_data.append(track['artist'][0])
track_data.append(track['album'][0])
return track_data
I tried to to do
track_data = [track['title'][0], track['artist'][0], track['album'][0]]
but it didn't appear as a good solution for my IDE (PyCharm).
What should I use?
asked Feb 28, 2014 at 12:21
2 Answers 2
Here is what I do when I want to remember the Awesomeness of Python
track_data = [track[prop][0] for prop in ('title', 'artist', 'album')]
answered Feb 28, 2014 at 12:24
fields = ['title', 'artist', 'album']
track_data = [track[f][0] for f in fields]
answered Feb 28, 2014 at 12:24
lang-py