How to iterate through a list and search for a value - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to iterate through a list and search for a value (/thread-7737.html) |
How to iterate through a list and search for a value - fad3r - Jan-23-2018 Hi everyone, First time poster here. Been studying python about 2 weeks now. I am trying to loop through a list to check if a certain value is in it. The value will be random but will always contain a * at the front. Here is the code I am using to loop through: for x in stringsplit: if x = \*: print (x)I am getting this error from python: SyntaxError: invalid syntax And pycharm tells me there is a : expected in the if statement even though it already has one. I feel like in other languages I would use a contains function but this does not exist in python. I have spent around 3-4 hours searching the web and I have come back with nothing. Help appreciated. RE: How to iterate through a list and search for a value - Larz60+ - Jan-23-2018 pos = your_string.index('\*') RE: How to iterate through a list and search for a value - fad3r - Jan-23-2018 (Jan-23-2018, 01:49 AM)Larz60+ Wrote:pos = your_string.index('\*') I don't understand the answer. Here is the code as it stands now # Request user input to begin work string = input("Please input the output of the command for processing: ") stringsplit = string.split("\\n") # Let's look for that stupid * for x in stringsplit: if x == "*": print (x)Here is the information I am feeding the input command: * coach\n jc_rrm6\nas mter\n names\n Part of the error from post 1 looks like it was happening because I didnt have == I just had =. Warning messages wasn't really helpful. I think this doenst work either because x is not equal to *, it is equal to * coach and that is why I was trying to find subsitute for contains. I basically need to search X for a small snippet of code. I am guessing there are no inbuilt functions and I will have to use regular expressions maybe? I solved it for x in stringsplit: if "*" in x: print (x) |