Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (Retired) Lists Redux Pop

Onur Salman
Onur Salman
3,612 Points

I am trying to use two integer lists and add the numbers that are same in one list

I am trying to use two integer lists and add the numbers that are same in one list but I keep getting an indentation error message.

Thanks for the help. My code is below. I tried the snapshot but I couldn't paste it

num_list_1 = [ 1, 2, 3, 4]
num_list_2 = [ 4 ,2 ,5, 3 , 6]
common_list = []
different_list = []
for num1 in num_list_1:
   for num2 in num_list_2:
       if num1 == num2:
   common_list.append(num1)
return common_list

It's a clever idea. I indented the common list inside the if, replaced "return common_list" with "print(common_list)", and it worked perfectly :D

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Onur,

First and foremost, I recommend you have a read of Posting Code to the Forum as its a great reference when you need to share code on the forum.

Your code does have inconsistent indentations, Python expects all code execution to be indented by the same number of spaces in a consistent manner which makes it easier for the compiler to execute said code. Currently – your for and if statements inside your first for loop have 3 spaces which is great, but your append declaration falls back to the start of the second for loop which is the cause of the error.

To solve this, you simply just need to bring it across so it is 3 spaces further in than the if statement.

for num1 in num_list_1:
   for num2 in num_list_2:
       if num1 == num2:
           common_list.append(num1)

return common_list

Hope that helps.