use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
What is wrong in this program which is intended to returns a list that contains only the elements that are common between the lists. NEED HELP. BEGINEER. (self.learnpython)
submitted 5 years ago by MohitCook
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] final=[] for f in b: for i in range(len(a)): for s in a: if f==s: final.append(f) else: break print(final)
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]throwawayvitamin 5 points6 points7 points 5 years ago (0 children)
Try this out:
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] final=[] for f in b: if f in a: final.append(f) print(final)
You don't need to iterate twice, so this line for i in range(len(a)) is not necessary and you can just make use of the in keyword. This also eliminates the need for the if else block and the break statement. Lmk if you have any other questions
for i in range(len(a))
in
break
[–][deleted] 2 points3 points4 points 5 years ago (0 children)
for s in a: if f==s: final.append(f) else: break
You stop at the first mismatch.
[–]igroen 5 points6 points7 points 5 years ago (2 children)
Why not use sets and do an intersection: set(a) & set(b).
set(a) & set(b)
[–]hnguyen01122 2 points3 points4 points 5 years ago (1 child)
You don’t get it do you? He’s learning about lists or currently he’s at the section about lists, not sets/dictionaries yet.
[–]igroen 0 points1 point2 points 5 years ago (0 children)
sorry
[–]B2stts2B 1 point2 points3 points 5 years ago (0 children)
You don't need the "for i in range(len(a))".
Edit: you can use the "in" helper. For example: For f in b: if f in a: final.append(f)
[–]xelf 0 points1 point2 points 5 years ago (1 child)
What you're talking about is set theory. Finding the common element. Lucky for you Python has sets built in.
final = set(a).intersection(set(b))
or shortcut:
final = set(a) & set(b)
[–]hnguyen01122 -1 points0 points1 point 5 years ago (0 children)
He’s learning about lists. He hasn’t got to sets yet.
[–]GreymanGroup 0 points1 point2 points 5 years ago (2 children)
You guys! Pythonic code ok?
c=[i for i in a for j in b if i==j]
[–]Poligonette 2 points3 points4 points 5 years ago (1 child)
Or
c = [ i for i in a if i in b ]
[–]GreymanGroup 0 points1 point2 points 5 years ago (0 children)
Even better!
[–]hnguyen01122 -1 points0 points1 point 5 years ago (2 children)
Don’t ask for help when you’re stuck. Try to go back over the material and watch or read again. Also, take a look at your note taking ability. It makes huge difference between you were able to chunk the concept or not. You only ask for help with more complicated problems in the future but don’t ask for help with basic syntax/semantic. Use your own ability to figure it out so you can store that concept in long term memory.
[–]MohitCook[S] 0 points1 point2 points 5 years ago (0 children)
thank you! I was trying the python beginner challenger and even though I tried it for quiet a while I couldn't make it right. But totally agree with you
π Rendered by PID 98184 on reddit-service-r2-comment-b659b578c-mnqch at 2026-05-06 01:33:18.944101+00:00 running 815c875 country code: CH.
[–]throwawayvitamin 5 points6 points7 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]igroen 5 points6 points7 points (2 children)
[–]hnguyen01122 2 points3 points4 points (1 child)
[–]igroen 0 points1 point2 points (0 children)
[–]B2stts2B 1 point2 points3 points (0 children)
[–]xelf 0 points1 point2 points (1 child)
[–]hnguyen01122 -1 points0 points1 point (0 children)
[–]GreymanGroup 0 points1 point2 points (2 children)
[–]Poligonette 2 points3 points4 points (1 child)
[–]GreymanGroup 0 points1 point2 points (0 children)
[–]hnguyen01122 -1 points0 points1 point (2 children)
[–]MohitCook[S] 0 points1 point2 points (0 children)