+ 14
Does someone uses tuples in python?
Since that the contents of it are immutable, can you actually consider using it instead of list? Any examples on how to use it?
11 odpowiedzi
+ 16
Raisa Ramos Excellent question. While I don't develop in Python, I am quite familiar with it as well as being familiar with use cases for immutable data structures.
If I was developing in Python, I would consider the following use cases for tuples:
- Readonly Data and Shared Objects:
- Memory Loaded App Config Settings
- Log messages / Event logging
- Audit data
- Request Headers and Payload
- Unidirection data flow design patterns
- Memory Cache of Objects
- Pure Functions
- Event store
- Undo/redo logic
I'm sure this list can go on for quite a while.
Tuples would be useful where systems are sensitive to side effects resulting from data changes in shared objects... and well, there are other various scenarios we could explore. Hopefully this satisfies your question.
+ 7
I use Python for a lot of machine learning lately and there are basically 3 cases in which I use tuples:
1. I want to try a function with different parameters. For example (0,1,10,100,1000) or (1,2,3,5,7,10,15,20).
Tuples are more efficient than lists and if I want to change the list/tuple of parameters, I do it manually anyway. Using range() for iterating obviously isn't suitable in my case.
2. You probably know that you can return 2 values in a Python function - if you have variables x and y, you can return both by writing "return x, y". However, if you only want y, you need to store both return values somewhere when calling the function. But not if you return a tuple. z = func()[1] works like a charm then. If I write z = func(), z will contain the whole tuple. Which leads me to 3.
3. Sometimes variables are strongly connected. For example the mean error and the standard deviation of a statistical model. Instead of declaring 2 variables, I just save both in a tuple and carry that through the code.
+ 2
Your choice if you want
+ 2
When you think about nested structures, you can also easily find use cases where tuples or immutable structures can be meaningful. One example: to represent points (x, y) in a coordinate system. In this case you may use a tuple of two integers or floats for the point itself, and build a list, a set, or even a dictionary from these tuples, depending on the use case.
Immutable structures tend to be a lot faster than lists, so when you deal with a lot of data, then performance differences will matter.
+ 2
Tuple packing and unpacking is a notable feature that is very useful. Well, not exactly at the beginning, but later on when one moves onto slightly more complex coding.
+ 1
Tuples are relatively easy to use because you can't change them. So when I have data sets that don't change, I'd use tuples, otherwise lists mostly.
+ 1
Michael David By packing and unpacking, would the use of named tuples with dot notation be an example of what you are referring to?
https://code.sololearn.com/cz9q6M621LSy/?ref=app
If I was actively building applications in Python, I imagine this would be a common practice I would use.
0
Tuples is immutable
we can also access tuples by using index value
tuple is very fast when comparing to lists