0

How can I print the item that is found? (without for-loop?) I want to find elements of array 1 in array 2 and then print those elements.

check = any(item in p for item in words)
print(check) # Result is TRUE

3 Answers 3

2

You can also use set intersection:

x = ['1', '2', '3']
y = ['2', '3', '4']

print(set(x).intersection(y))


output: {'2', '3'}

You can find more info on intersection here, note a set cannot contain any duplicates. I wasn't sure if that is a requirement.

Sign up to request clarification or add additional context in comments.

Comments

1

Try

empty_lst = [print(item) for item in words if item in p]

empty_lst will store [] To store the cross-over items, do;

cross_items = [item for item in words if item in p]

The reason your question doesnt work is that item in p will always return a boolean value so item in p if item in words returns a list of True and False. if item in p checks for the item. Look up tutorials in list comprehension to learn more.

Comments

1

I don't really think yo can do that. any returns a Boolean value

word = ["your list"]
numbers = ["numbers you want to check for"]
for i in word:
    if i in numbers:
        print(i)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.