New to Python

Nanjundi nanjundi at gmail.com
Mon Mar 12 22:48:08 CET 2007


On Mar 12, 4:49 am, "Bert Heymans" <bert.heym... at gmail.com> wrote:
> On Mar 12, 3:02 am, Alberto Vieira Ferreira Monteiro
>
> <albm... at centroin.com.br> wrote:
> > Hi, I am new to Python, how stupid can be the questions I ask?
>
> > For example, how can I add (mathematically) two tuples?
> > x = (1,2)
> > y = (3,4)
> > How can I get z = (1 + 3, 2 + 4) ?
>
> > Alberto Monteiro
>
> Alberto -
>
> List comprehesion, no doubt about it:>>> z = [k+p for k,p in (x, y)]

To put the correct form of the soulution here.
This will give the desired output z = [k+p for k,p in zip(x, y)]
Or z = [k+p for k,p in map(None, x, y)]
>>> z
[4, 6]

-N




More information about the Python-list mailing list