Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using class set value to access a list subset

The testList example represents list strings from a text file. The compareList represents a values list from a dictionary. I want to ‘pull out’ subsets of a string AFTER the match.

# Use a set result to print "fox here!"
testList = ['quick', 'brown', 'fox', 'here!']
compareList = ['beige', 'black', 'brown']
foundList = []

result = set(testList) & set(compareList)
# <class 'set'> {'brown'}

How to use the compareList value ‘brown’ to print the remainder of the testList eg ['fox', 'here!'] From a resultant foundList

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Get the index of brown and then slice the rest of the list.

color = list(result)[0]
index = testList.index(color)
rest = testList[index+1:]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading