-1

I have two sets like A={"sara", "peter", "ray"} and B={"ram", "sara", "gouri"}. I want to take one member of list B (for example "sara") and check with list A to see if this name exists in the list or not. If this name exists then print "yes". I want to check all the members in list B with list A. I have the code below but it doesn't work.

for i in B:
   if B[i]==A:
      print("yes")
Deli
  • 25
  • 6
  • Are you sure `{...}` is a *list*? That looks like a set to me. – iBug May 06 '21 at 16:41
  • @iBug you are right. I will edit my post. – Deli May 06 '21 at 16:42
  • 1
    Does this answer your question? [Fastest way to check if a value exists in a list](https://stackoverflow.com/questions/7571635/fastest-way-to-check-if-a-value-exists-in-a-list) – Fester May 06 '21 at 16:47

2 Answers2

5

It's easier than you think:

for i in B:
   if i in A:
      print("yes")

This assumes that both A and B are lists, but you seem to have dictionaries/sets. You might need to clarify that first. If you have sets, the above solution should still work.

Edit: you now say that both A and B are columns from two separate DataFrames. In that case you can do:

A={"sara", "peter", "ray"} 
B={"ram", "sara", "gouri"}

df1 = pd.DataFrame(A, columns=['Names'])
df2 = pd.DataFrame(B, columns=['Names'])

for index, row in df1.iterrows():
    if row['Names'] in df2.Names.tolist():
        print('yes')

Edit 2: you now say that you want to add the result to a new column in df2. Use:

A={"sara", "peter", "ray"} 
B={"ram", "sara", "gouri"}

df1 = pd.DataFrame(A, columns=['Names'])
df2 = pd.DataFrame(B, columns=['Names'])

df2['present_in_df1'] = np.where(df1['Names'] == df2['Names'], "yes", "no")

Output df2:

    Names   present_in_df1
0   gouri   no
1   ram     no
2   sara    yes
sander
  • 1,340
  • 1
  • 10
  • 20
  • they are two columns in dataframe. column A in one dataframe and column B in another dataframe. – Deli May 06 '21 at 16:48
  • @Sanderjuhh Thanks but it returns 'yes ' for all of them. It should return 'yes' only for "sara". – Deli May 06 '21 at 17:12
  • @Deli if you copy and run the code from my edit above, it will only print "yes" for the row with Sara. – sander May 06 '21 at 17:41
  • yes but when I'm trying to save the results in a new column in df2, it returns "yes" for all. I'm writing this df2['result']="yes" instead of print ('yes') – Deli May 06 '21 at 17:56
  • @Deli see edit again. In the future, please explain your question better in your original post, so that users don't have to edit their questions based on your -later added- requirements. – sander May 06 '21 at 18:07
  • sure. Just one more question? this is only works when two columns have same size? how we can handle when columns are not same size? – Deli May 06 '21 at 19:11
0

I think you're looking for the in keyword. Breaking this down:

(for example "sara") and check with list A to see if this name exists in the list or not. If this name exists then print "yes"

if "sara" in B:
    print("yes")

I want to check all the members in list B with list A.

for b in B:
    if b in A:
        print("yes")
Thomas Matecki
  • 629
  • 5
  • 20