1

I have identical tuples of (0, 1) assigned to define limits for 3 input values:

bounds = ((0, 1), (0, 1), (0, 1))

Is there a Pythonic way to assign same tuples for N inputs? For example:

bounds = ((0, 1), (0, 1), (0, 1), (0, 1), (0, 1), ...Nth(0, 1))
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
artDeco
  • 470
  • 2
  • 8
  • 21
  • Thanks @Jean-François Fabre and jonrsharpe for your remarks. That question "How do you create a list of repeated tuples in Python?" is very hard to understand for my purpose. I'd like to keep this question as focused and specific as it is not a duplicate. – artDeco Sep 11 '17 at 21:21
  • 1
    it is a duplicate, but the answers given here are good & upvoted, so use the ones here (Peter answer is very good). But it _is_ a duplicate (maybe we can find better ones to add to the list) – Jean-François Fabre Sep 11 '17 at 21:22
  • Thanks @Jean-François Fabre appreciate your input :) – artDeco Sep 11 '17 at 21:23
  • 1
    when closing as a duplicate, I feel the duty not to be too hard on users. This isn't a punishment (I even upvoted your question because it's much clearer than the "original" question), this is helping them / helping the site to avoid more answers to be posted. Peter's answer is excellent and was posted before closure: that's fine by me. – Jean-François Fabre Sep 11 '17 at 21:25
  • Understood @Jean-François Fabre yes agree :) appreciate your help! – artDeco Sep 11 '17 at 21:28

4 Answers4

7

You can multiply a sequence to get N shallow copies of its contents:

bounds = ((0, 1),) * n

This is fine for tuples of ints or other immutable data structures containing only immutable types, but will cause surprising behavior if you use it for mutable data structures like lists - you get a sequence of n references to the same list, because it's a shallow copy. In that case, a comprehension is the most idiomatic way to create n independent objects:

mutable_bounds = [[0, 1] for _ in range(n)]
Peter DeGlopper
  • 36,326
  • 7
  • 90
  • 83
2
bounds = ((0, 1),) * N

Works for any iterable, BTW: '1111' == '1' * 4.

bipll
  • 11,747
  • 1
  • 18
  • 32
  • works _properly_ for any _immutable_ iterable. For mutable iterables, it repeats the same reference over again: not what anyone would want. – Jean-François Fabre Sep 11 '17 at 21:18
  • For immutables as well, I presume, and every time you forbid someone to want something, god kills a smoothly running process. :( – bipll Sep 11 '17 at 21:21
  • it repeats the references for tuples, yes, but you cannot change a tuple in the list without changing the reference: no risk of changing all elements by changing one. Try `x = [[]] * 10` then `x[0].append(1)` and see what I'm talking about (https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – Jean-François Fabre Sep 11 '17 at 21:24
  • @Jean-FrançoisFabre Trust me I know what you're talking about, and IMO no language's legal semantics can be deemed unwantable by everyone. – bipll Sep 11 '17 at 22:32
2

itertools.repeat() alternative:

import itertools

n = 5    # coefficient
bounds = tuple(itertools.repeat((0,1), n))
print(bounds)

The output:

((0, 1), (0, 1), (0, 1), (0, 1), (0, 1))
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
2

You can use the multiplication operator (*) on lists of tuples. For example:

((0,1),) * 3 

yields:

((0, 1), (0, 1), (0, 1))
Fejs
  • 2,734
  • 3
  • 21
  • 40