Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: (sub) String equivalence vs List fast membership testing

Tags:

python

Can somebody explain to me the following (python 2.7)

Two string numbers, from a parsed file:

'410.9' '410.9 ' (Notice the trailing space)

A_LIST = ['410.9 ']

'410.9' in '410.9 '
True

'410.9' in A_LIST
False

No problem working around this - just trying to understand why it is so.

Thanks!

like image 772
chris Avatar asked Dec 05 '22 21:12

chris


1 Answers

in with two strings checks or a substring, whereas in with a list checks for membership.

What you want is something like [x for x in A_LIST if '419' in x]

like image 124
Yannick Versley Avatar answered Feb 19 '23 22:02

Yannick Versley