How can I create a non-literal python tuple?


You can first construct a list then change the single value you want to change, then finally convert that to a tuple if you want to create a non-literal python tuple. For example,

def create_non_literal_tuple(a, b, c):
   x = [1] * a
   x[c] = b
   return tuple(x)
create_non_literal_tuple(6, 0, 2)

This will give the output:

(1, 1, 0, 1, 1, 1)

A 0 in position 2 of an array of length 6.

Published on 18-Apr-2018 14:08:59