techman
techman

Reputation: 433

check if string exist inside a list

I have a list:

test = [{u'TopicArn': u'arn:aws:sns:us-east-1:700257:test1'},
  {u'TopicArn': u'arn:aws:sns:us-east-1:700257:test2'},
  {u'TopicArn': u'arn:aws:sns:us-east-1:700257:test3'},
  {u'TopicArn': u'arn:aws:sns:us-east-1:700257:test4'}]

And I want to check if a string exists in this above list.

string = "{u'TopicArn': u'arn:aws:sns:us-east-1:700257:test4'}"

if string in test:
   print("string exist in list")
else:
   print("string dont exist in list")

My variable string ha a string that exist inside the list but Im getting the message "string dont exist in list".

Do you understand why?

Upvotes: 3

Views: 1837

Answers (5)

use a list comprehension and compare the values for a pattern match.

 test = [{u'TopicArn': u'arn:aws:sns:us-east-1:700257:test1'},
  {u'TopicArn': u'arn:aws:sns:us-east-1:700257:test2'},
  {u'TopicArn': u'arn:aws:sns:us-east-1:700257:test3'},
  {u'TopicArn': u'arn:aws:sns:us-east-1:700257:test4'}]

 string = 'arn:aws:sns:us-east-1:700257:test4'

 result=list(d for d in test if string in d.values())
 print(result)

output:

 [{'TopicArn': 'arn:aws:sns:us-east-1:700257:test4'}]

Upvotes: 0

Stefan Pochmann
Stefan Pochmann

Reputation: 28606

These are the four things in test:

{u'TopicArn': u'arn:aws:sns:us-east-1:700257:test1'}
{u'TopicArn': u'arn:aws:sns:us-east-1:700257:test2'}
{u'TopicArn': u'arn:aws:sns:us-east-1:700257:test3'}
{u'TopicArn': u'arn:aws:sns:us-east-1:700257:test4'}

"arn:aws:sns:us-east-1:700257:test4" is not one of them, and the in comparison doesn't look deeper.

What you probably want to do is something like this:

if any(string in d.values() for d in test):

That checks whether the string is in the values of one of the dictionaries in test:

>>> string = "arn:aws:sns:us-east-1:700257:test4"
>>> string in test
False
>>> any(string in d.values() for d in test)
True

Upvotes: 6

jgritty
jgritty

Reputation: 11925

You have a list of dictionaries, try this:

found = False
for adict in test:
    if string in adict.values():
       found = True
if not found:
    print("string dont exist in list")
else:
    print("string in list")

It seems what you really want to know is if the string is one of the values in one of the dictionaries in the list.

A more pythonic one-liner was given as a comment:

if any(d['TopicArn'] == string for d in test):
    print("string in list")

Upvotes: 2

Tuan Anh Hoang-Vu
Tuan Anh Hoang-Vu

Reputation: 1995

The Python in function does not work that way. You need to convert your test into a list or set of strings first:

test2 = set([x['TopicArn'] for x in test])

Upvotes: 3

rafaelc
rafaelc

Reputation: 59274

Because you have dicts inside the list. You have to iterate over the list and check if the string is in the dict values.

string = "arn:aws:sns:us-east-1:700257:test4"

for d in test:
    if string in d.values():
       print("string exist in list")
    else:
       print("string dont exist in list")

If you just ask if string in test , it will check if the string "arn:aws:sns:us-east-1:700257:test4" in one of the elements in the list. That is not True.

Upvotes: 1

Related Questions