Posts: 27
Threads: 8
Joined: Sep 2017
Hi, I got some lists, i would like to "bring together" 2 , 3 ,or 4 lists and make only one list from them ,i have to use *args(its an exo)
Here is my code:
list1 = [1,2]
list2 = [1,3,5,7,9]
list3 = [1,2,3,4]
list4 = [7,2,4]
def mergelist(*args):
[fill?]
print mergelist(list2, list2)
# >>> [1,3,5,7,9,1,3,5,7,9]
print mergelist(list2, list3, list4)
# # >>> [1,3,5,7,9,1,2,3,4,7,2,4]
#
print mergelist(l1)
# # >>> [1,2] after " # ", are expected outputs
any help ?
Posts: 8,158
Threads: 160
Joined: Sep 2016
def mergelist(*args):
result = []
for arg in args:
result.extend(arg)
return result def mergelist2(*args):
return [item for arg in args for item in arg]
Posts: 27
Threads: 8
Joined: Sep 2017
Sep-25-2017, 01:02 PM
(This post was last modified: Sep-25-2017, 01:03 PM by haye.)
Hello Buran, thanks for helping.
with:
def mergelist(*args):
result = []
for arg in args:
result.extend(arg)
return result my output is :
Output: [[1, 3, 5, 7, 9], [1, 3, 5, 7, 9]]
[[1, 3, 5, 7, 9], [1,2,3,4], [7,2,4]]
[[1,2]]
but I would like it to be :
Output: [1,3,5,7,9,1,3,5,7,9]
[1,3,5,7,9,1,2,3,4,7,2,4]
[1,2]
Posts: 8,158
Threads: 160
Joined: Sep 2016
well this looks like append, not extend... please, check your code
Posts: 27
Threads: 8
Joined: Sep 2017
Sep-25-2017, 01:07 PM
(This post was last modified: Sep-25-2017, 01:09 PM by haye.)
here is my code:
list1 = [1,2]
list2 = [1,3,5,7,9]
list3 = [1,2,3,4]
list4 = [7,2,4]
def mergelist(*args):
r = []
for arg in args:
r.extend(arg)
return r
print mergelist(list2, list2)
# >>> [1,3,5,7,9,1,3,5,7,9]
print mergelist(list2, list3, list4)
# # >>> [1,3,5,7,9,1,2,3,4,7,2,4]
#
print mergelist(l1)
# # >>> [1,2]
Posts: 8,158
Threads: 160
Joined: Sep 2016
This code does not much the output from your previous post. note that return r is one more level indented than in my snippet.
def mergelist(*args):
result = []
for arg in args:
result.extend(arg)
return result
list1 = [1,2]
list2 = [1,3,5,7,9]
list3 = [1,2,3,4]
list4 = [7,2,4]
print mergelist(list2, list2)
# [1,3,5,7,9,1,3,5,7,9]
print mergelist(list2, list3, list4)
#[1,3,5,7,9,1,2,3,4,7,2,4]
print mergelist(list1) Output: mergelist
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
[1, 2]
My code
def mergelist(*args):
result = []
for arg in args:
result.extend(arg)
return result
list1 = [1,2]
list2 = [1,3,5,7,9]
list3 = [1,2,3,4]
list4 = [7,2,4]
print mergelist(list2, list2)
# [1,3,5,7,9,1,3,5,7,9]
print mergelist(list2, list3, list4)
#[1,3,5,7,9,1,2,3,4,7,2,4]
print mergelist(list1) Output: mergelist
[1, 3, 5, 7, 9, 1, 3, 5, 7, 9]
[1, 3, 5, 7, 9, 1, 2, 3, 4, 7, 2, 4]
[1, 2]
And with append
def mergelist(*args):
result = []
for arg in args:
result.append(arg)
return result
list1 = [1,2]
list2 = [1,3,5,7,9]
list3 = [1,2,3,4]
list4 = [7,2,4]
print mergelist(list2, list2)
# [1,3,5,7,9,1,3,5,7,9]
print mergelist(list2, list3, list4)
#[1,3,5,7,9,1,2,3,4,7,2,4]
print mergelist(list1) Output: mergelist
[[1, 3, 5, 7, 9], [1, 3, 5, 7, 9]]
[[1, 3, 5, 7, 9], [1, 2, 3, 4], [7, 2, 4]]
[[1, 2]]
>>>
Posts: 1,298
Threads: 38
Joined: Sep 2016
Did you insert buran's code correctly? When I add his solution to your code (correcting line 17 to read list1 instead of l1):
list1 = [1, 2]
list2 = [1, 3, 5, 7, 9]
list3 = [1, 2, 3, 4]
list4 = [7, 2, 4]
def mergelist(*args):
result = []
for arg in args:
result.extend(arg)
return result
print(mergelist(list2, list2))
# >>> [1,3,5,7,9,1,3,5,7,9]
print(mergelist(list2, list3, list4))
# >>> [1,3,5,7,9,1,2,3,4,7,2,4]
print(mergelist(list1))
# >>> [1,2] I get:
Output: [1, 3, 5, 7, 9, 1, 3, 5, 7, 9]
[1, 3, 5, 7, 9, 1, 2, 3, 4, 7, 2, 4]
[1, 2]
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 27
Threads: 8
Joined: Sep 2017
|