How to do [1]*4 for a tuple

Discussion in 'Python' started by Vineet Jain, Apr 6, 2004.

  1. Vineet Jain

    Vineet Jain Guest

    I'm not sure I understand why [1]*4 and (1)*4 work differently? [1]*4
    results in [1, 1, 1, 1] while (1)*4 results in 4. There are times when I
    have to do the following:

    '%s some value %s and some other text %s' % (a)*3

    as apposed to

    '%s some value %s and some other text %s' % (a, a, a)

    In this case I always end up doing

    '%s some value %s and some other text %s' % [a]*3

    VJ
    Vineet Jain, Apr 6, 2004
    #1
    1. Advertising

  2. Vineet Jain wrote in news:mailman.375.1081213535.20120.python-
    :

    > I'm not sure I understand why [1]*4 and (1)*4 work differently? [1]*4
    > results in [1, 1, 1, 1] while (1)*4 results in 4. There are times when
    > I have to do the following:
    >


    >>> (1,) * 4

    (1, 1, 1, 1)
    >>>



    > '%s some value %s and some other text %s' % (a)*3
    >


    >>> a = "<aaa>"
    >>> '%s some value %s and some other text %s' % ((a,)*3)

    '<aaa> some value <aaa> and some other text <aaa>'



    > as apposed to
    >
    > '%s some value %s and some other text %s' % (a, a, a)
    >
    > In this case I always end up doing
    >
    > '%s some value %s and some other text %s' % [a]*3
    >


    tuples are comma seperated items, parenthesis just bracket:

    >>> b = 1, 3
    >>> b

    (1, 3)
    >>> type( b )

    <type 'tuple'>
    >>> c = 1,
    >>> c

    (1,)
    >>> type( c )

    <type 'tuple'>
    >>>


    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Apr 6, 2004
    #2
    1. Advertising

  3. On Mon, 5 Apr 2004 21:05:34 -0400, "Vineet Jain" <>
    wrote:

    >I'm not sure I understand why [1]*4 and (1)*4 work differently? [1]*4
    >results in [1, 1, 1, 1] while (1)*4 results in 4. There are times when I
    >have to do the following:
    >
    >'%s some value %s and some other text %s' % (a)*3
    >
    >as apposed to
    >
    >'%s some value %s and some other text %s' % (a, a, a)
    >
    >In this case I always end up doing
    >
    >'%s some value %s and some other text %s' % [a]*3


    You have the answer to your question already, but you may be better
    off using a dictionary with the % operator to get named association...

    >>> a={'a':'aaa'}
    >>> print '%(a)s some value %(a)s and some other text %(a)s' % a

    aaa some value aaa and some other text aaa

    This works particularly well when not all substituted strings are the
    same, and when you may want to change format strings in such a way
    that the substitutions may not be in the same order, or may not all be
    used, etc. In other words, it can be very useful when the format
    strings are supplied externally.

    It can be handy when the support guys want to decide the wording of
    error messages (and change their minds every other day) for instance.


    --
    Steve Horne

    steve at ninereeds dot fsnet dot co dot uk
    Stephen Horne, Apr 6, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Mikolajczyk
    Replies:
    1
    Views:
    807
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    953
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    612
    Bill Scherer
    Apr 20, 2004
  4. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    821
    Antoon Pardon
    Mar 30, 2005
  5. Steve
    Replies:
    1
    Views:
    962
    Fredrik Lundh
    Dec 13, 2005
Loading...

Share This Page