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 darwinType "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 210 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_ADD10 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 natural to implement BUILD_TUPLE_UNPACK
faster than BINARY_ADD
because BINARY_ADD
is a polymorphic operator, requiring additional type calculation and implicit conversion.
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.
It's as easy as the following:
info_1 = "one piece of info"info_2 = "another piece"vars = (info_1, info_2)# 'vars' is now a tuple with the values ("info_1", "info_2")
However, tuples in Python are immutable, so you cannot append variables to a tuple once it is created.
" 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.]
As other answers have noted, you cannot change an existing tuple, but you can always create a new tuple (which may take some or all items from existing tuples and/or other sources).
For example, if all the items of interest are in scalar variables and you know the names of those variables:
def maketuple(variables, names):return tuple(variables[n] for n in names)
to be used, e.g, as in this example:
def example():x = 23y = 45z = 67return maketuple(vars(), 'x y z'.split())
of course this one case would be more simply expressed as (x, y, z)
(or even foregoing the names altogether, (23, 45, 67)
), but the maketuple
approach might be useful in some more complicated cases (e.g. where the names to use are also determined dynamically and appended to a list during the computation).
I'm pretty sure the syntax for this in python is:
user_input1 = raw_input("Enter Name: ")user_input2 = raw_input("Enter Value: ")info = (user_input1, user_input2)
once set, tuples cannot be changed.
If you tryna use a separate function then try either of these:
def insert_value_at_beginning(input_tuple, value_to_insert):return (value_to_insert,) + input_tupleinput_tuple = (2, 3, 4)value_to_insert = 1output_tuple = insert_value_at_beginning(input_tuple, value_to_insert)print(output_tuple) # Expected output: (1, 2, 3, 4)
OR
def insert_value_at_the_end(input_tuple, value_to_insert):return input_tuple + (value_to_insert,)input_tuple = (2, 3, 4)value_to_insert = 1output_tuple = insert_value_at_the_end(input_tuple, value_to_insert)print(output_tuple) # Expected output: (2, 3, 4, 1)
OR simply try either of these:
input_tuple = (2, 3, 4)value_to_insert = 1new = (value_to_insert, ) + input_tupleprint(new) # Expected output: (1, 2, 3, 4)
OR:
input_tuple = (2, 3, 4)value_to_insert = 1new = input_tuple + (value_to_insert, )print(new) # Expected output: (2, 3, 4, 1)