Eliminate "extra" variable

Gary Herron gary.herron at islandtraining.com
Fri Dec 6 18:28:20 EST 2013


On 12/06/2013 11:37 AM, Igor Korot wrote:
> Hi, ALL,
> I have following code:
>
> def MyFunc(self, originalData):
>       data = {}
>       dateStrs = []
>       for i in xrange(0, len(originalData)):
>             dateStr, freq, source = originalData[i]
>             data[str(dateStr)]  = {source: freq}
>             dateStrs.append(dateStr)
>      for i in xrange(0, len(dateStrs) - 1):
>            currDateStr = str(dateStrs[i])
>            nextDateStrs = str(dateStrs[i + 1])
>
>
> It seems very strange that I need the dateStrs list just for the
> purpose of looping thru the dictionary keys.
> Can I get rid of the "dateStrs" variable?
>
> Thank you.

You want to build a list, but you don't want to give that list a name?  
Why not?  And how would you refer to that list in the second loop if it 
didn't have a name?

And concerning that second loop:  What are you trying to do there? It 
looks like a complete waste of time.  In fact, with what you've shown 
us, you can eliminate the variable dateStrs, and both loops and be no 
worse off.

Perhaps there is more to your code than you've shown to us ...

Gary Herron




More information about the Python-list mailing list