Skip to content

Instantly share code, notes, and snippets.

@h3xh4wk
Last active June 4, 2016 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h3xh4wk/5611724 to your computer and use it in GitHub Desktop.
Save h3xh4wk/5611724 to your computer and use it in GitHub Desktop.
python list comprehensions
# search elements of one list in another list without nested loops
tosearch = ['x','z',10]
tobesearched = ['x','y',1,2,3]
found = [item
for item in tosearch
for item2 in tobesearched if item == item2]
print found
@h3xh4wk
Copy link
Author

h3xh4wk commented May 20, 2013

IMO, we should use this only when the lists to loop are really not that big.

@h3xh4wk
Copy link
Author

h3xh4wk commented Jul 18, 2013

it worked for searching the lists too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment