## I've got a silly question. ####################### P1 = "prefix1" P2 = "prefix2"
def my_func(list, items): s = 0 out = "" for i in range(len(list)): if s == 0: p = P1 s = 1 else: p = P2 s = 0 for j in range(len(items)): out += p +items[j] return out ########################
If my input was: list = ["car list 1","car list 2"] items = ["torino","mustang","elantra"]
for output I get: prefix1torinoprefix1mustangprefix1elantraprefix1torinoprefix1mustangprefix1elantra
when I expect: prefix1torinoprefix1mustangprefix1elantraprefix2torinoprefix2mustangprefix2elantra
Why not just do this:
####################### prefixes = ["prefix1" , "prefix2" ]
def my_func(list, items):
out = ""
for prefixloop in prefixes:
for itemloop in items:
out += prefixloop + itemloopreturn out ########################
It's what's called a 'nested loop' - and that way if you had to add a 3rd prefix, it would "just work" instead of having to add yet another variable to the loop you'd designed.
By the way, other than getting the length of 'list' you don't use anything in that list. Another thing (which might also be a problem in your code) using 'list' as a variable name is not a good idea, as it's a reserved word, if I'm not mistaken. Consider this snippet:
Python 2.2.2 (#1, Feb 24 2003, 19:13:11) [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> q = (1,2,3) >>> print list(q) [1, 2, 3]
Hope this helps, Roger "Merch" Merchberger
-- Roger "Merch" Merchberger | "Profile, don't speculate." SysAdmin, Iceberg Computers | Daniel J. Bernstein [EMAIL PROTECTED] |
_______________________________________________ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
