3 ms·
for animal, color in zip(['dog', 'cat', 'bird'], ['brown', 'black', 'red']): print 'See the %s %s?' % (color, animal)
by eurleif 14y ago
for animal, color in zip(['dog', 'cat', 'bird'], ['brown', 'black', 'red']):
print 'See the %s %s?' % (color, animal)
- jostmey 14y agoI never knew about this zip command. I guess it brings the same utility offered by the TCL foreach loops to python. Still, I wish more languages directly imitated the TCL foreach loop. I've found that when I code, I wish I could just do some simple task 3-4 times. I am always tempted to create a little function, and to call that function repeatedly. But that seems like overkill. With TCL, I just write a simple loop statement instead of the function. I've found that my code is more compact, and the number of functions that I declare is reduced.
- thomc 14y agoRuby too: ['dog', 'cat', 'bird'].zip(['brown', 'black', 'red']).each {|animal, color| puts "See the #{color} #{animal}?"}