News for hackers. Stuff that matters.
Re:Hidden features of Python by RyanMcGreal on Wednesday July 14, 2010 @08:26AM (#1514356)
>Wrap an iterable with enumerate and it will yield the item along with its index.

Now that's a feature I wish I knew about sooner. I've been using:

    for item in a:
	print a.index(item), item
which has always felt kludgy.
Re:Hidden features of Python by eru on Wednesday July 14, 2010 @03:10PM (#1515612)
Yes, enumerate is useful for that. But even if you did not now about it you could have used count from itertools to get the same effect:

  import itertools
  for (index, item) in zip(itertools.count(0), "hallo"):
      print index, item
Itertools is worth checking out for other goodies, too.
Re:Hidden features of Python by infinite8s on Thursday July 15, 2010 @05:39PM (#1519311)
Before enumerate, the standard idiom for this was:

    for i in range(len(a)):
        print i, a[i]
Re:Hidden features of Python by petdog on Wednesday July 14, 2010 @10:05AM (#1514576)
Seriously? You must enjoy your O(n^2).
Re:Hidden features of Python by RyanMcGreal on Wednesday July 14, 2010 @10:28AM (#1514635)
The sets I'm working on tend not to tend toward infinity.
Re:Hidden features of Python by eru on Wednesday July 14, 2010 @03:12PM (#1515620)
Yes. But the approach with index only works for actual sets. And if you have actual sets, you might as well use Python's built-in sets.