Q:

python lists , need find a string and later slice that string get the attached value to it

description=""" am id: asdm12
             store : 231
             mob  : 00000000"""

if any("am id:" in line for line in dataList ):
    amid_list= line.split(":")
    newAccMap['AMID'] = amid_list[1]
    popup(newAccMap['AMID'])
    print "AM ID:",amid_list[1]

else:
    print " NO AMID"
    popup("No AMID")

so this is my code: I want "AM ID:" value , I have used description.splitlines() function and stored it in list . Now in that list I need to search for " AM ID:" if it is present I need get value connected to that colon . else print no am id .

A:

You could use a for/else loop:
for line in description.splitlines():
    key, val = line.split(":")
    if key.strip() == "am id":
        print(val.strip())
        break
else:
    print("no am id")

Here, the else part is executed only if the loop terminated normally, i.e. without hitting the break.

