4

I had the list with following strings below

some_list = ['9196358485','9966325645','8846853128','8-4-236/2','9-6-32/45','Need to fetch some strings']

From the above strings i want only strings that does n't start with 91,9,8 but want strings starting with 8-, 9-

so below is my code

[i for i in some_list if all(not i.startswith(x) for x in ['91','8','9'])]

result:

['Need to fetch some strings']

In the above by using ['91','8','9'] as the condition is deleting the strings starting with 9 and 8 which is correct, but i don't want 9-, 8- also to be removed from the list, actually my intension is if the strings starting with 9 and 8 should be ignored as above and strings starting with 9- and 8- should not be ignored , can we write two conditions in a single line with concept of taking strings starting with 8-,9- and ignoring when strings starts with 9 or 8 in the above code i had written.

Can anyone please let me know hoe to do this.............

Edited code:

Thanks for all of u r support if u don't think this is another question i had some actual output on which the below code is not working

some_list = ['Mr K V  Prasad Reddy(MD)',
 '+(91)-9849633132, 9959455935',
 '+(91)-9849633132',
 'Near NRI College,Opp Vijaya Bank,Nizam Pet Road,Nizampet,Hyderabad - 502102',
 '9196358485',
 '9966325645', 
 '8846853128',
 '8-4-236/2',
 '9-6-32/45',
 'Need to fetch some strings']

When i apply the bwlow code using regex i got following output result:

['Mr K V  Prasad Reddy(MD)',
 '+(91)-9849633132, 9959455935',
 '+(91)-9849633132',
 'Near NRI College,Opp Vijaya Bank,Nizam Pet Road,Nizampet,Hyderabad - 502102',
 '8-4-236/2',
 '9-6-32/45',
 'Need to fetch some strings']

Actually i don't want all phone numbers from the list, so they will be in the above format sometimes starting with 91 sometimes 8 sometimes 9

How can we remove all those phone numbers from the list?

2
  • yes in the regular expression given below how to avoid string starting with +(91) Commented Aug 7, 2012 at 8:53
  • I mean how to ignore strings staring with +(91) and 9 and 8 Commented Aug 7, 2012 at 9:00

1 Answer 1

6

Use a regular expression:

>>> import re
>>> [i for i in some_list if not re.match(r"[98]\B|+\(91\)", i)]
['8-4-236/2', '9-6-32/45', 'Need to fetch some strings']

\B matches only within alphanumeric strings, so it matches between 9 and 1 but not between 9 and -.

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

3 Comments

If you explicitly want to check for a dash after 9 or 8, you could also use the regex [98](?!-).
Actually that worked but i had pasted a real example above can u please view that
by using the above code only the strings starting with 8 and 9 are ignored but what if i also the strings starting with +(91) need to be ignored

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.