Page 1 of 1

## 3 Replies - 15842 Views - Last Post: 06 February 2011 - 09:46 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=214465&amp;s=84df0a822d7d8bece2ce6f86ff09a8fe&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 naufilah

Reputation: 0
• Posts: 4
• Joined: 17-May 10

Posted 06 February 2011 - 09:15 PM

HI there,
this problem seems to be easy but i cant seem to find a solution to it.

list1 = [1,2,3]
list2 = [4,5,6]
# result should be [5,7,9]

i have tried coming up with the following code but,theres still a error.

list_1 = [1,2,3]
list_2 = [4,5,6]

if __name__ == "__main__":

may i know how to solve this and come up with the result.
someone help out...

Is This A Good Question/Topic? 0

## Replies To: add two lists

### #2 Dogstopper

Reputation: 2956
• Posts: 11,220
• Joined: 15-July 08

Posted 06 February 2011 - 09:30 PM

This is as simple as doing a list comprehension and using the zip function. What you can do is iterate over both lists and sum up the result of the two numbers. The zip function takes two lists and returns a list a tuples.

```list3 = [(x + y) for x, y in zip(list1, list2)]

```

Thus, for the first number, the zip() method returns (1, 4) which, when added is 5. Then the next tuple returned is (2,5), which when added is 7. Thus you are making a single list out of two.

### #3 naufilah

Reputation: 0
• Posts: 4
• Joined: 17-May 10

Posted 06 February 2011 - 09:45 PM

Hi there,

I have tried adding the code to the code like this...and when i run the code,theres no error displayed at the same time,theres no output.pls look back into my codes and help me out.Thanks lots!

list_1 = [1,2,3]
list_2 = [4,5,6]
list3 = [(x + y) for x, y in zip(list_1, list_2)]

if __name__ == "__main__":

### #4 Dogstopper

Reputation: 2956
• Posts: 11,220
• Joined: 15-July 08