What I Learned at Work this Week: A Tuple with One Element

Mike Diaz
3 min readOct 7, 2023

--

Photo by Pixabay: https://www.pexels.com/photo/close-up-of-text-247781/

This past week, I was tagged in a Slack thread about a Python syntax issue. I read the code, commented that I couldn’t see anything wrong with it, and offered to pair on it later if the problem wasn’t solved. Another, less senior engineer, mentioned that it might be missing a comma, which I disagreed with.

They were right, so the first lesson I learned this week was how to graciously acknowledge that I was wrong (I like to think this was more of a refresher since I already knew how to do that). But thanks to this other engineer and their willingness to explain, I learned something new about Python! Miniblog incoming:

The Data

The code in question was specifically throwing an error when one piece of data was passed into a function. Here’s the code I was shown:

   subscriber_headers = [
"subscriber_id",
"external_id",
"email",
"phone_number",
"first_name",
"last_name",
"source",
"status",
]
user_profile_growth_parts_list = ({'subscriber_id':subscriber_id, 'external_id': subscriber.external_id, "email": subscriber.email, "phone_number": subscriber.phone, "first_name": subscriber.first_name, "last_name": subscriber.last_name, "source": subscriber.source, "status": subscriber.status})

create_and_move_file(
subscriber_id,
subscriber_headers,
SUBSCRIBER_INFO_FILENAME,
user_profile_growth_parts_list,
'subscriber data',
folder_name,
)

I didn’t know what was inside create_and_move_file , but it turned out not to be relevant. The issue was with user_profile_growth_parts_list. Let’s reprint it to make it more clear:

user_profile_growth_parts_list = (
{
'subscriber_id':subscriber_id,
'external_id': subscriber.external_id,
"email": subscriber.email,
"phone_number": subscriber.phone,
"first_name": subscriber.first_name,
"last_name": subscriber.last_name,
"source": subscriber.source,
"status": subscriber.status
}
)

The first thing that jumped out at us was the mixed use of quotation marks — some were ' and others were ". We changed them to all be single quotes just to be safe, but also confirmed that Python doesn’t care which type we use. In researching, I did learn that Generally, double quotes are used for string representation and single quotes are used for regular expressions, dict keys or SQL. Good to know, I’ll try to remember that convention in the future.

Tuple

So it’s not the quotes, but my coworker also called out that there might be a missing comma here. Where? the other two of us asked. Right here:

user_profile_growth_parts_list = (
{
'subscriber_id':subscriber_id,
'external_id': subscriber.external_id,
'email': subscriber.email,
'phone_number': subscriber.phone,
'first_name': subscriber.first_name,
'last_name': subscriber.last_name,
'source': subscriber.source,
'status': subscriber.status
}, <-----WE NEEDED THIS COMMA HERE
)

When I was reading the code, I didn’t realize that this dictionary was wrapped in parentheses because it’s supposed to be a tuple of dictionaries. A tuple is a collection, like a list, but unlike a list it is immutable. One other unique property is that a tuple requires a comma, even when it only has one element.

This is critical for distinguishing a tuple from an expression simply spread over multiple lines. If we are putting something inside of parentheses, we must add a comma at the end, or Python will not recognize it as a tuple. Naturally, the function we were passing user_profile_growth_parts_list to was expecting a tuple, so adding the comma solved everything else.

This was a shorter mystery than what I usually have to solve, but it still had us scratching our heads. Besides this Python quirk, it’s important to remember to explore every avenue and listen to others when they make a suggestion. It’ll make you look smarter when they turn out to be right!

Sources

--

--