Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to use *args
#1
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 ?
Reply
#2
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]
Reply
#3
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]
Reply
#4
well this looks like append, not extend... please, check your code
Reply
#5
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]
Reply
#6
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]] >>>
Reply
#7
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
Reply
#8
fixed, thanks !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to compare two parameters in a function that has *args? Milan 4 1,262 Mar-26-2023, 07:43 PM
Last Post: Milan
  *args implementation and clarification about tuple status amjass12 10 4,021 Jul-07-2021, 10:29 AM
Last Post: amjass12
  [SOLVED] Good way to handle input args? Winfried 2 2,055 May-18-2021, 07:33 PM
Last Post: Winfried
  Two Questions, *args and //= beginner721 8 3,486 Feb-01-2021, 09:11 AM
Last Post: buran
  does yield support variable args? Skaperen 0 1,670 Mar-03-2020, 02:44 AM
Last Post: Skaperen
  is there a way: repeat key word args Skaperen 2 2,234 Feb-03-2020, 06:03 PM
Last Post: Skaperen
  Using function *args to multiply multiple arguments allusernametaken 8 6,064 Nov-20-2019, 12:01 AM
Last Post: allusernametaken
  Passing string args to Popen CardBoy 3 4,239 Jan-16-2018, 09:22 AM
Last Post: Gribouillis
  Why args type is always tuple, when passed it as argument to the function. praveena 5 5,319 Jan-16-2018, 09:07 AM
Last Post: praveena
  subprocess with args haye 0 4,423 Oct-23-2017, 10:54 AM
Last Post: haye

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020