Dictionaries

John Gordon gordon at panix.com
Thu Mar 20 10:19:50 EDT 2014


In <mailman.8300.1395322251.18130.python-list at python.org> ishish <ishish at domhain.de> writes:

> The script [...] only creates batch1.csv.

If the script only creates batch1.csv, that means Batch2 and Batch3 must
be empty.

> for k, v in myDict.items():
> 	if Batch1.has_key(k):
> 		if k in Batch2.has_key(k):
> 			Batch3[k] = v
> 		else:
> 			Batch2[k] = v
> 	else:
> 		Batch1[k] = v

'if k in Batch2.has_key(k):' is a very strange line of code.  In fact, it
should produce a syntax error if it were ever executed, because the 'in'
keyword requires an iterable as the second part, and has_key() returns
only True or False.

Therefore, I would say that line of code never executes, which means
that the preceding 'if Batch1.has_key(k)' statement always evaluates
to False.

Which therefore means that Batch2 and Batch3 never accumulate any items,
matching your observed output.

-- 
John Gordon         Imagine what it must be like for a real medical doctor to
gordon at panix.com    watch 'House', or a real serial killer to watch 'Dexter'.




More information about the Python-list mailing list