Data Structures in Python: tuples and sets
One thing that astonished me when I stated digging into the world of Python was it’s rich support for data structures. While PHP had only arrays (well, SPL allows much more, but who uses SPL?), Python has not only lists. While it supports things like bytearray and buffer, I was mostly attracted to tuples and sets.
So lets get going!
Tuples are very much like lists. They both keep elements in order and you can access them same. Definition differs that tuples don’t need any kind of brackets. Check this example:
l = ['I', 'am', 'Python', 'list'] # list definition t = 'I', 'am', 'Python', 'tuple' # tuple definition print l # prints "Python" print t # prints "Python"
You may raise a question “why do I need two same data structures?” as I did. Well, there is one difference: tuples are immutable. That means, once you have a tuple, it stays like that until it is dropped out of memory.
t = 'I', 'am', 'Python', 'tuple' t = 'list' # TypeError: 'tuple' object does not support item assignment
I found it very useful that you can unpack tuples. One application is functions. Let’s say I want to have function, that calculates measurements of central tendency (median, mode and mean). Take a look at this example:
def calculate_central_tendency_measurements(items): # Let's assume that we have these three different functions, # which calculate different measurements median = calculate_median(items) mode = calculate_mode(items) mean = calculate_mean(items) return median, mode, mean items =  # List with items # Most obvious way is to set variables to items of tuple one by one stats = calculate_central_tendency_measurements(items) median = stats mode = stats mean = stats # A lot shorter way median, mode, mean = calculate_central_tendency_measurements(items)
Limits are defined by your imagination.
Sets in Python are same as in mathematics: they are unordered and contain no duplicate values. It is very useful for fast testing if member exists in our data. Sets are created from lists.
items = ['I', 'am', 'Python', 'list'] set_of_items = set(items) assert 'I' in set_of_items assert 'random' not in set_of_items
Not much to say about that. If you often have to check membership, then use sets. It should speed up things. Maybe I will test that one and write about it. 🙂
Python has a lot to offer for a developer like me. And it is very elegant (I love the syntax!).
What data structures of Python you use the most?