Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • \$\begingroup\$ a.index(b)==0 is a bit shorter. Alternatively, you could do 0**sum(a.index(b)for a in l for b in l). \$\endgroup\$ Commented May 2, 2016 at 7:46
  • \$\begingroup\$ @Mego That doesn't work because index throws an exception when b isn't found. And because it should be ==, not >=. However, find works. (And it's shorter too!) \$\endgroup\$ Commented May 2, 2016 at 7:54
  • \$\begingroup\$ Whoops, I meant to type find. Sleepy brain is sleepy. The second version should also work with find. \$\endgroup\$ Commented May 2, 2016 at 8:13
  • \$\begingroup\$ @Mego I'm not sure if I get the second version. Wouldn't that always return 0? \$\endgroup\$ Commented May 2, 2016 at 8:14
  • \$\begingroup\$ @Mego That only works if every string is the same. The reason we compare it to len(l) is since we're iterating through all bs on each a, there will always be at least one match per a. So we check if the number of matches is the same as the number of elements. \$\endgroup\$ Commented May 2, 2016 at 8:32