Using TUPLES in Python

Senior Data Engineer • Contractor / Freelancer • GCP & AWS Certified
Search for a command to run...

Senior Data Engineer • Contractor / Freelancer • GCP & AWS Certified
No comments yet. Be the first to comment.
Concise guides to Python features and patterns most useful for data engineering — from built-ins to standard library modules.
Using the right tool for the job is central to programming. This holds when considering what data structure to use for a particular problem. Python offers a variety of built-in data structures, and one of the most versatile among them is the set. Let...
Here's a useful Dataform concept: pre_operations and post_operations. As the name implies, these represent a set of actions that run before and after the main operation (table, view, or SQL operations

BigQuery has always been a SQL engine for tabular data. Object tables add an interesting twist to that. Instead of rows containing values, an object table gives you one row per file — pointing at da

Query your data lake with warehouse-grade security and performance — without moving a single file.

Ever run a heavy BigQuery SQL query, processed gigabytes of data — and then accidentally closed the tab or forgot to save the results? 😬 Don't re-run it. Your results are still there. BigQuery automa

You can use query parameters in BigQuery hashtag#SQL (now in the console as well!) — but how are they different from variables, and when should you use each? Both parameters and variables act as place

Continuing my posts about Python collections (see post about sets), let's look at another important data structure in Python - the tuple.
So when is it used and how does it differ from a list?
🌟 Strengths of Tuples:
- Immutable: Once created, they cannot be modified. This makes them hashable and usable as keys in dictionaries.
- Memory Efficient: Tuples are generally more memory-efficient than lists.
- Safety: Since they are immutable, there’s no risk of unintended side-effects when passing them around in functions.
🚫 Weaknesses of Tuples:
- Limited Flexibility: You can't add, remove, or modify elements after creation.
- Fewer Methods: They come with fewer built-in methods compared to lists.
🔍 When to use Tuples:
- When you need a collection of data that shouldn't change, like the days of the week or coordinates of a point.
- When you want to ensure data integrity and prevent accidental modifications.
- As keys for dictionaries.
✨ Differences from Lists:
- Mutability: Lists are mutable, tuples are not.
- Syntax: Lists use square brackets [] and tuples use parentheses ().
- Methods: Lists have several methods like append(), remove(), and extend(), which tuples do not (given their immutable nature).
# Creating a Tuple
container_size = (5.0, 10.0)
# Accessing Elements
print(container_size[0]) # Outputs: 5.0
# Tuples in Dictionaries
locations = {(40.7128, -74.0060): 'New York', (51.5008, -0.1177): 'London'}
# List vs Tuple
example_list = [1, 2, 3]
example_type = (1, 2, 3)
example_list[1] = 4 # Valid
# my_tuple[1] = 4 # Throws an error
Tuples are great in their domain of applications! But like every tool, they have their place. Know when to use them over lists and leverage their strengths.
Happy coding! 🚀
Found it useful? Subscribe to my Analytics newsletter at notjustsql.com.