Question or problem about Python programming:
I am learning Python and creating a database connection.
While trying to add to the DB, I am thinking of creating tuples out of information and then add them to the DB.
What I am Doing:
I am taking information from the user and store it in variables.
Can I add these variables into a tuple? Can you please help me with the syntax?
Also if there is an efficient way of doing this, please share…
EDIT
Let me edit this question a bit…I only need the tuple to enter info into the DB. Once the information is added to the DB, should I delete the tuple? I mean I don’t need the tuple anymore.
How to solve the problem:
Solution 1:
Tuples are immutable; you can’t change which variables they contain after construction. However, you can concatenate or slice them to form new tuples:
a = (1, 2, 3) b = a + (4, 5, 6) # (1, 2, 3, 4, 5, 6) c = b[1:] # (2, 3, 4, 5, 6)
And, of course, build them from existing values:
name = "Joe" age = 40 location = "New York" joe = (name, age, location)
Solution 2:
You can start with a blank tuple with something like t = ()
. You can add with +
, but you have to add another tuple. If you want to add a single element, make it a singleton: t = t + (element,)
. You can add a tuple of multiple elements with or without that trailing comma.
>>> t = () >>> t = t + (1,) >>> t (1,) >>> t = t + (2,) >>> t (1, 2) >>> t = t + (3, 4, 5) >>> t (1, 2, 3, 4, 5) >>> t = t + (6, 7, 8,) >>> t (1, 2, 3, 4, 5, 6, 7, 8)
Solution 3:
Another tactic not yet mentioned is using appending to a list, and then converting the list to a tuple at the end:
mylist = [] for x in range(5): mylist.append(x) mytuple = tuple(mylist) print mytuple
returns
(0, 1, 2, 3, 4)
I sometimes use this when I have to pass a tuple as a function argument, which is often necessary for the numpy functions.
Solution 4:
In Python 3, you can use *
to create a new tuple of elements from the original tuple along with the new element.
>>> tuple1 = ("foo", "bar") >>> tuple2 = (*tuple1, "baz") >>> tuple2 ('foo', 'bar', 'baz')
The byte code is almost the same as tuple1 + ("baz",)
Python 3.7.5 (default, Oct 22 2019, 10:35:10) [Clang 10.0.1 (clang-1001.0.46.4)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> def f(): ... tuple1 = ("foo", "bar") ... tuple2 = (*tuple1, "baz") ... return tuple2 ... >>> def g(): ... tuple1 = ("foo", "bar") ... tuple2 = tuple1 + ("baz",) ... return tuple2 ... >>> from dis import dis >>> dis(f) 2 0 LOAD_CONST 1 (('foo', 'bar')) 2 STORE_FAST 0 (tuple1) 3 4 LOAD_FAST 0 (tuple1) 6 LOAD_CONST 3 (('baz',)) 8 BUILD_TUPLE_UNPACK 2 10 STORE_FAST 1 (tuple2) 4 12 LOAD_FAST 1 (tuple2) 14 RETURN_VALUE >>> dis(g) 2 0 LOAD_CONST 1 (('foo', 'bar')) 2 STORE_FAST 0 (tuple1) 3 4 LOAD_FAST 0 (tuple1) 6 LOAD_CONST 2 (('baz',)) 8 BINARY_ADD 10 STORE_FAST 1 (tuple2) 4 12 LOAD_FAST 1 (tuple2) 14 RETURN_VALUE
The only difference is BUILD_TUPLE_UNPACK
vs BINARY_ADD
. The exact performance depends on the Python interpreter implementation, but it’s easier to implement BUILD_TUPLE_UNPACK
fast than BINARY_ADD
because BINARY_ADD
is a polymorphic operator, requiring additional type calculation and implicit conversion.
Solution 5:
” once the info is added to the DB, should I delete the tuple? i mean i dont need the tuple anymore.”
No.
Generally, there’s no reason to delete anything. There are some special cases for deleting, but they’re very, very rare.
Simply define a narrow scope (i.e., a function definition or a method function in a class) and the objects will be garbage collected at the end of the scope.
Don’t worry about deleting anything.
[Note. I worked with a guy who — in addition to trying to delete objects — was always writing “reset” methods to clear them out. Like he was going to save them and reuse them. Also a silly conceit. Just ignore the objects you’re no longer using. If you define your functions in small-enough blocks of code, you have nothing more to think about.]