1

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
0

2 Answers 2

3

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
2
fields = ['title', 'artist', 'album']
track_data = [track[f][0] for f in fields]
answered Feb 28, 2014 at 12:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.