Using two lists as two arguments in a function


#1

Hi! Anybody know how to solve this?
m = [1, 2, 3]
n = [4, 5, 6]

def join_lists(x,y):
result=0
for i in range(len(x,y)):
result=result+x[i]+y[i]
return result
print join_lists(m, n)

You want this to print [1, 2, 3, 4, 5, 6]


#2

hi why dont you do this part

def join_lists(x,y):
 result=0
 for i in range(len(x,y)):
 result=result+x[i]+y[i]
 return result

like that

def join_lists(x, y) : 
    result = x + y
    return result

#3


can anyone pls tell me whats wrong here. Thank you


#4

HI I think that you should return inside the function join_list() the argument x and y

def join_lists(x, y):
    return x + y

#5

Hi All,

I have used the below code and it works fine,

m = [1, 2, 3]
n = [4, 5, 6]

Add your code here!

def join_lists(x, y):
All = []
for i in range(len(x)):
mm = x
for i in range(len(y)):
mmm = y
All = mm + mmm
return All
print join_lists(m, n)

You want this to print [1, 2, 3, 4, 5, 6]

mm and mmm are variables that I have added to help me.


#6

You can also have your result like this and it works:

m = [1, 2, 3]
n = [4, 5, 6]

def join_lists(x, y):
result = []
for i in x:
k1 = x
for i in y:
k2 = y
result = k1 + k2
return result
print join_lists(m, n)


#7

You can also use this;

def join_lists(x, y):
result = 0
for i in (x,y):
result = x+y
return result


#8

I did it like this (without result=[]) to make it shorter:

m = [1, 2, 3]
n = [4, 5, 6]

def join_lists(x, y):
for i in (x,y):
result = x+y
return result

print join_lists(m, n)