Jussi Piitulainen wrote: > Ravi Prabakaran writes: > >> I'm completely new to python. I just need simple logic to get output >> without any loops. >> I have list of string and list of list of numbers. >> Each string should be concatenated with every third and fourth >> values to generate proper titles in list of strings. >> >> t = ['Start','End'] >> a = [[1,2,3,4], >> [5,6,7,8]] >> >> >> Expected Result : ( list of strings ) >> >> ['Start - 3 , End - 4', >> 'Start - 7 , End - 8'] >> >> Note : First 2 values from each list should be ignored. >> >> >> Could anyone please guide me with best solution without loops ? > > That's a strange requirement - to have repetition without loops, in > Python, and still have a best solution. > > I suppose it's fine to have a (built-in) function do the looping for > you so that there is no explicit loop in your own code. The .format > method of Python strings can do each individual string: > > list(map('{2} - {0} , {3} - {1}' > .format('{0[2]}', '{0[3]}', *t) > .format, a))
Don't do that if t may contain user data. For the sake of the argument let's assume that a[...][0:2] is confidential. Then >>> t = "{0[0]}", "{0[1]}" >>> list(map('{2} - {0} , {3} - {1}' ... .format('{0[2]}', '{0[3]}', *t) ... .format, a)) ['1 - 3 , 2 - 4', '5 - 7 , 6 - 8'] (I think doubling the braces is sufficient to fix this) > The first (inner) call of .format builds the actual format string > whose .format method then builds each output string: {0[2]} in a > format string refers to the argument position 0 and its element > position 2; *t spreads the two elements of t as further positional > arguments. -- https://mail.python.org/mailman/listinfo/python-list