Quantcast
Channel: Add variables to tuple - Stack Overflow
Viewing all articles
Browse latest Browse all 10

Answer by AbdulHafeez for Add variables to tuple

$
0
0

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)

Viewing all articles
Browse latest Browse all 10

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>