Stop Using Lists for Everything in Python

An introduction to tuples and sets

Jonathan Hsu
Jan 13 · 3 min read
Photo by Vitor Santos on Unsplash

When you’re learning something new, you’re unfamiliar with all the possibilities and once something’s effective, you tend to stick with it… in Python, that something is lists.

I’ve been guilty of this — lists are so convenient, syntactically clear, and flexible. Add in the pure joy of learning about list comprehensions and you feel like you’ve hit the data type jackpot.

Using lists can feel like endlessly spamming your favorite special move in Smash Bros.

As with many things Python, there are absolute gems, hidden in plain sight, that will level-up your skill as a Pythonista. Two of those gems are tuples and sets.

Let’s go over each of these special data types and find out why you should be using them instead of lists.


Tuples

The syntax for using tuples is near-identical to lists except that instead of square brackets, parentheses are used. Additionally, you can take a list and convert it to a tuple.

# how to define a list
num_list = [1,2,3,4]
# how to define a tuple
num_tuple = (1,2,3,4)
# use tuple() to convert
num_convert = tuple(num_list)

What’s so special about being immutable? It might seem like an inconvenience at first; however, you’re doing two things every time you appropriately use a tuple instead of a list.

  • Writing more semantic and safe code. When you define a variable as a tuple, you’re telling yourself and any other viewer of your code: “This does not change”. Just in case you miss the memo, any attempts to modify the variable will be met with an error.
  • Improving performance. Iterating over tuples will be faster than lists. Tuples are more memory-efficient than lists. Since the number of items in the tuple does not change, its memory footprint is more concise.

If you have lists whose size is not being modified or whose purpose is solely for being iterated over, try replacing it with a tuple.


Sets

To define a set, use curly braces to enclose a comma-separated list of items. Do not confuse this with creating a dictionary, which has key-value pairs. As with tuples, a set can be created by converting another data type.

# how to define a list
num_list = [1,2,3,4]
# how to define a set
num_set = {1, 2, 3, 4}
# use set() to convert
num_convert = set(num_list)

So, what happens if two items are identical?

nums = {1,2,3,4,4}
print(nums) # 1,2,3,4

As you can see, the second 4 is removed. The same would happen if our original value was a list of duplicated items.

So, why should you be using sets instead of lists? First of all, converting to a set is the easiest way to remove duplicate values. Additionally, sets—just like any data type—have their own set of methods.

Sets are great when comparing multiple sets—think of a Venn Diagram. The union(), intersection(), and difference() functions will respectively tell you the combined, shared, and distinct values between two sets.


Conclusion

While lists are comfortable, dependable, and learned early on, there may be a better tool.

Begin using tuples for faster processing and protecting your declared data structures. Begin using sets for ensuring unique values and leveraging comparison methods.

Better Programming

Advice for programmers.

Jonathan Hsu

Written by

I love to share my experiences learning to code, working with customers, and managing my blog. Sign up for my newsletter at https://j-hsu.com

Better Programming

Advice for programmers.

More From Medium

More from Better Programming

More from Better Programming

More from Better Programming

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade