flat tuple

W

Will McGugan

Hi,

What is the simplest way of turning a single value and a tuple in to a
single 'flat' tuple?

ie.

t= ( 1, 2, 3 )
n= 10

n, t gives me ( 10, ( 1, 2, 3 ) )

Fair enough, but I would like to get.. ( 10, 1, 2, 3 )

I would like to use the result to create a string with the % operator -
I was hoping there was some shorthand to produce ( n, t[0], t[1], t[2] ).

Thanks,

Will McGugan
 
P

Phil Frost

Tuples can be joined with the + operator, like so:
(1, 2, 3, 4)

so in your case, you could do (n,) + t. If you are using the result for
% formatting, it might be better to use a list, which is mutable, so
it's possible to add a new element without constructing a new list, like
so:
[10, 1, 2, 3]

If you have many things to concatenate, you can use the builtin function
reduce to do it, like so:
l = [[1,2,3],[4,5],[6]]
reduce(lambda a, b: a.extend(b) or a, l, [])
[1, 2, 3, 4, 5, 6]


Hi,

What is the simplest way of turning a single value and a tuple in to a
single 'flat' tuple?

ie.

t= ( 1, 2, 3 )
n= 10

n, t gives me ( 10, ( 1, 2, 3 ) )

Fair enough, but I would like to get.. ( 10, 1, 2, 3 )

I would like to use the result to create a string with the % operator -
I was hoping there was some shorthand to produce ( n, t[0], t[1], t[2] ).

Thanks,

Will McGugan
 
D

Duncan Booth

Will said:
What is the simplest way of turning a single value and a tuple in to a
single 'flat' tuple?

ie.

t= ( 1, 2, 3 )
n= 10

n, t gives me ( 10, ( 1, 2, 3 ) )

Fair enough, but I would like to get.. ( 10, 1, 2, 3 )

I would like to use the result to create a string with the % operator -
I was hoping there was some shorthand to produce ( n, t[0], t[1], t[2] ).

Tuples support concatenation so all you have to do is to wrap n in a tuple
then add them together:

format % ((n,) + t)
 
A

Alex Martelli

Will McGugan said:
Hi,

What is the simplest way of turning a single value and a tuple in to a
single 'flat' tuple?

ie.

t= ( 1, 2, 3 )
n= 10

n, t gives me ( 10, ( 1, 2, 3 ) )

Simplest is probably:

(n,) + t


Alex
 
A

Alex Martelli

Phil Frost said:
Tuples can be joined with the + operator, like so:

(1, 2, 3, 4)

so in your case, you could do (n,) + t. If you are using the result for
% formatting, it might be better to use a list, which is mutable, so

Might be, but % wants a tuple as the RHS (assuming the LHS format string
has multiple %-formatting elements), NOT a list.


Alex
 
J

John Lenton

Might be, but % wants a tuple as the RHS (assuming the LHS format string
has multiple %-formatting elements), NOT a list.

you mean it actually converts it into a tuple?

--
John Lenton ([email protected]) -- Random fortune:
Most public domain software is free, at least at first glance.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFBUIrxgPqu395ykGsRAgMAAKClv5TnjUbODvNpNByW+ZbqVkB+oACePKfQ
dX/+dUoUWrKyTX+rTpIA3UQ=
=fgU1
-----END PGP SIGNATURE-----
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top