Edit: I’ve realized my error (if statement, variable x is assigned to element in string and i’m comparing to length of list).. Trying to resolve that tomorrow morning. Appologies for the stupid error but i would appreciate any bits of learning.
I’m trying to combine two lists into a single list.
m = [1,2,3]
n = [4,5,6]
v = m+n
def myFun():
return [(str(x)+str(y)) for x in m if x < len(m) for y in n if y < len(n)]
print(myFun())
the result of myFun() should display “14, 25, 36”
i also tried to breakdown the code into a more pythonic world and have seen where i’ve gone astray:
def my(fun()):
for x in m if x < len(m):
for y in n if y < len(n): # problem here, running until count 9 instead of 3
# like it's supposed to.. author error..
newlist.append(str(x)+str(y))
print(newlist)
Am i even headed in the right direction or should i be trying to build a map, i’ve seen a few pages saying that maps could be counter-productive if you have to reverse with a list-comp or lambda? Also, is it possible to count a specific object inside a list comprehension / lambda? (e.g. list length)?
use
zip():or use
itertools.izip_longest()if the lists are of different lengths: