pythontuples

creating a non-literal python tuple programmatically


I want to create a tuple of length m, with a 1 in each position except for one n in position k.

e.g.: m=5, n=7, k=3 should yield (1,1,1,7,1) (length 5 with a 7 in position 3)

How can I do this?


Solution

  • >>> m, n, k = 5, 7, 3
    >>> tuple(n if i == k else 1 for i in range(m))
    (1, 1, 1, 7, 1)