0

In Python, how do I check if two lists contain identical items, and return a list of those items, or alternatively return True if there are matches?

Given:

list1=[1,2,3,4,5]
list2=[2,4,6,8]

How can I return:

list3=[2,4]

or use check if it's true, as in:

if MATCHES EXIST:
    DO SOMETHING

Also, how would I concatenate the items of a list into a single sequence or string?

Given:

list1=[1,2,3,4,5]

How can I return:

list2=[12345]
3
  • 1
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the FAQ and How to Ask. Commented Dec 29, 2016 at 7:24
  • What have you tried so far? To help us help you, please add a Minimal, Complete, and Verifiable example Commented Dec 29, 2016 at 7:26
  • NO please do not think in that way i have tried to check if the number that user enter in my list Commented Dec 29, 2016 at 13:46

2 Answers 2

9

In Python, You can get the similar items from two different list using set. That will give you the values which are same in the two different lists.

>>> a=[1,2,3]
>>> b=[2,3,4]
>>> set(a) & set(b)
{2, 3}
>>> 

To get this output in the list format, Just Type as follows

>>> list(set(a) & set(b))
    [2, 3]

You can make the lists from a string value using list("123") command

>>> a="123"
>>> list(a)
['1', '2', '3']
>>> 
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your help and what if i would like to do it by useing İF and ELSE like list1=[6,7,8,9,10] list2=[1,2,3,4,5] and than get input from the user like x=int(input('enter your age :')) than if the number that user enter in my list1 print ('you are older than my')
@jon-yilmaz It's not entirely clear what you are asking in your comment here, though it seems like an entirely new question. On StackOverflow, you should always post each question independently as a new question, so in the future people can search for just that question. With that said, are you looking for something like the following?: list1 = [10,11,15,17,20,37] user_age = int(input('enter your age :') ) if user_age in list1: print( "There is at least one other person this age." ) else: list1.append( user_age ) print( "You are the first person of this age!" )
1

Your first question has been answered well by @k-suthagar, so I'll defer to him on that one. Though here's slightly different approach that performs well, returning a new list:

list1=[1,2,3,4,5]
list2=[4,2,3,9,9]
list3 = list( set( list1 ).intersection( set( list2 ) ) )

If you simply want to check if there are any matches, you can do this:

if set( list1 ).intersection( set( list2 ) ):
    print( "These lists contain some identical elements." )
else:
    print( "These lists do NOT contain identical elements." )

As to your second question, you can do the following:

list1=[1,2,3,4,5]
list2 = [ int( ''.join(str(x) for x in list1) ) ]
print( list2 )

[12345]

If you wish to join strings, or for the result to be a string, simply drop the int coercion:

list1=[1,2,3,4,5]
list2 = [ ''.join(str(x) for x in list1) ]
print( list2 )

NOTE: It's generally good practice on StackOverflow to ask one question per post, and to show us what you have tried.

1 Comment

thank you this one work for my cod

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.