Search string in a list data = ['HTTP/1.1 200 OK', 'CACHE-CONTROL: max-age=1810', 'DATE: Wed, 14 May 2014 12:15:19 GMT', 'EXT:', 'LOCATION: http://192.168.94.57:9000/DeviceDescription.xml', 'SERVER: Windows NT/5.0, UPnP/1.0, pvConnect UPnP SDK/1.0', 'ST: uuid:7076436f-6e65-1063-8074-78542e239ff5', 'USN: uuid:7076436f-6e65-1063-8074-78542e239ff5', 'Content-Length: 0', '', '']


From the above list, I have to extract the .xml link.

My code:

for element in data:
    if 'LOCATION' in element:
        xmllink = element.split(': ').[1]


It's taking too much time.  How can I make this faster?

Actually I am doing SSDP discovery for finding devices in a network.  After sending the M-SEARCH command, devices send a datagram packet which I have taken in a data variable.  From this I have to extract the file link of that device for processing it.

When I use indexing to extract, it was done quickly.
You want to test for element.startswith('LOCATION: ').  You are doing 'LOCATION' in element, which is not only slower since it has to check at every position of every element, it might also lead to false matches.

Also, element and data are poor names.  I suggest header and headers, respectively.

My suggestion:

LOC = 'LOCATION: '
xmllinks = [header[len(LOC):] for header in headers if header.startswith(LOC)]
if xmllinks:
    xmllink = xmllinks[0]