How ChatGPT4 is helping me improve my Python

Brianna Lytle
4 min readMar 22, 2023
Photo by Christina @ wocintechchat.com on Unsplash

I gave ChatGPT my code for a Spotify Scraper I was writing and it provided me with 6 tips on how to improve it.

As somebody who has been part of the massive Q4 2022 layoffs, I’ve been frantically trying to get myself back into the habit of refreshing my skillset including practicing Python code. So, I spent hours week after week trying to find solutions to the projects I was working on, priding myself on the fact that I will teach myself the hard way and not copy and paste from others' portfolios let alone, ChatGPT (young me was so naive).

This past week, a mentor of mine told me to start using it and treat it as a personal analyst. So I did. I asked ChatGPT how I can improve my code for a Spotify scraper I was writing. Here are 6 things it taught me (end of blog compares the two codes):

1. Use Descriptive Variable Names

Try to use variable names that describe what they represent — I fall victim to this whenever I start a new project and use words that describe the current coding mindset like ‘eek’ or ‘ugh’ or ‘pls’.

2. Break down Code into smaller Functions

I tend to create multiple operations to understand my own thinking. The current code is quite long and performs multiple operations. Try breaking it down into smaller, reusable functions that can be tested individually. This makes the code easier to read, understand, and maintain.

3. Break down Code into smaller Functions

Instead of multiple ‘for’ loops to append items to a list, list comprehensions can make code shorter and more concise.

For example, instead of eek = [] and for al in album_items: al_dict = {...} eek.append(al_dict),

You can use a list comprehension like eek = [{...} for al in album_items if not any(d['album_name'] == al['name'] for d in eek) and al['album_type'] != 'single'].

4. Avoid using ‘iloc’

Using iloc to access a specific row or column in a DataFrame can be slow and may not always return what you expect. Instead, use boolean indexing to filter the DataFrame or use the loc accessor to access rows by label

5. Use ‘pd.concat’ to combine DataFrames

Instead of using a for loop to concatenate DataFrames, you can use pd.concat to combine them into a single DataFrame. This is usually faster and more memory-efficient

6. Use try-except blocks

When accessing external APIs, it’s always a good idea to use try-except blocks to handle errors. This prevents the code from crashing if there is an issue with the API request.

To Sum up!!

My work became faster and deeper than’s ChatGPT and saved me a lot of time from scrolling on StackOverflow. I am still the owner of my thought process of what I want to execute — ChatGPT is my assistance to executing it in a more thoughtful manner. Most importantly, ChatGPT helped me recognize areas of improvement while picking up on best practices in python.

PS. Final code will be published once I finish building the project for my portfolio.

Stay tuned and stay connected for more.

APPENDIX

Code I Inputted To ChatGPT4

Here is the code I inputted with notes of where the code will improve based on ChatGPT4’s tips

# Tip 2 - Breaking down code into smaller functions (this whole code - whew!)
eek = [] #Tip 1 - Descriptive Name
album_items = sp.artist_albums(artist_uri)['items']
for al in album_items: #Tip 3 - List Comprehensions
al_dict = {
'album_name': al['name'],
'album_release' : al['release_date'],
'total_tracks': al['total_tracks'],
'album_type': al['album_type'],
'album_uri': al['id']
}
if not any(d['album_name'] == al_dict['album_name'] for d in eek) and al_dict['album_type'] != 'single':
eek.append(al_dict)

df = pd.DataFrame(eek)
df.head()

oi = [] #Tip 1 - Descriptive Name
for album_uri in df['album_uri']: #Tip 3 - List Comprehensions
#Tip 4 - Avoid using iloc
album_name = df.loc[df['album_uri'] == album_uri, 'album_name'].iloc[0]
for track in sp.album_tracks(album_uri)['items']:
album_dict = {
'album_name': album_name,
'track_num': track['track_number'],
'track_name': track['name'],
'duration_ms' : track['duration_ms'],
'track_uri': track['uri'],
}
oi.append(album_dict)

ugh = pd.DataFrame(oi, columns=['album_name', 'track_num', 'track_name', 'duration_ms', 'track_uri'])
ugh.head()

#Tip 6 - Try-except blocsk
audio_df = pd.DataFrame()
for z in range(len(ugh['track_uri'])): #Tip 5 - combining DataFrames
dict_audio = pd.DataFrame.from_dict(sp.audio_features(ugh['track_uri'][z]))
audio_df = pd.concat([audio_df, dict_audio], ignore_index = True)
audio_df.drop(audio_df.loc[:, 'type' : 'time_signature'].columns, axis = 1, inplace = True)

full_deets = pd.concat([ugh, audio_df], axis = 1, join = 'inner')
full_deets.head()

Improved Code Based on Tips Provided by ChatGPT4

def get_album_info(artist_uri):
album_items = sp.artist_albums(artist_uri)['items']
albums = []
for album in album_items:
album_dict = {
'album_name': album['name'],
'album_release' : album['release_date'],
'total_tracks': album['total_tracks'],
'album_type': album['album_type'],
'album_uri': album['id']
}
if not any(d['album_name'] == album_dict['album_name'] for d in albums) and album_dict['album_type'] != 'single':
albums.append(album_dict)
return pd.DataFrame(albums)

def get_track_info(album_uri):
tracks = []
album_name = df.loc[df['album_uri'] == album_uri, 'album_name'].iloc[0]
for track in sp.album_tracks(album_uri)['items']:
track_dict = {
'album_name': album_name,
'track_num': track['track_number'],
'track_name': track['name'],
'duration_ms' : track['duration_ms'],
'track_uri': track['uri'],
}
tracks.append(track_dict)
return pd.DataFrame(tracks)

def get_audio_features(track_uri):
try:
audio_features = sp.audio_features(track_uri)[0]
except:
audio_features = {}
return audio_features

artist_uri = 'some-artist-uri'
album_df = get_album_info(artist_uri)

track_df = pd.concat([get_track_info(album_uri) for album_uri in album_df['album_uri']])
audio_df = pd.DataFrame([get_audio_features(track_uri) for track_uri in track_df['track_uri']])
audio_df = audio_df.drop(['type', 'id', 'uri', 'track_href', 'analysis_url', 'time_signature'],

--

--