Python Forum
How to iterate through a list and search for a value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to iterate through a list and search for a value
#1
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.
Reply
#2
pos = your_string.index('\*')
Reply
#3
(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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  search a list or tuple for a specific type ot class Skaperen 8 1,853 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  Use one list as search key for another list with sublist of list jc4d 4 2,106 Jan-11-2022, 12:10 PM
Last Post: jc4d
  Search in an unsorted list amir_0402 2 12,168 Jun-04-2020, 10:25 PM
Last Post: deanhystad
  Alpha numeric element list search rhubarbpieguy 1 1,741 Apr-01-2020, 12:41 PM
Last Post: pyzyx3qwerty
  search binary file and list all founded keyword offset Pyguys 4 2,698 Mar-17-2020, 06:46 AM
Last Post: Pyguys
  Trying to iterate through a list... t4keheart 2 1,637 Feb-07-2020, 08:15 PM
Last Post: t4keheart
  Using Python to search through a list of urls jeremy 4 2,813 Dec-18-2019, 11:52 AM
Last Post: Malt
  list of lists iterate only sends the last value batchenr 1 1,869 Sep-24-2019, 07:04 AM
Last Post: Gribouillis
  Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV dn237 19 6,564 May-29-2019, 02:27 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020