Skip to content Skip to sidebar Skip to footer

How Do I Index A List Contained Inside *args?

having trouble getting a single value from the list in the 'funCall' function. Any idea how to index the *args? def funCall(*args): ans = args[0] print(ans) def roll(a_lis

Solution 1:

You need to pass check to funCall like so:

if len(check) ==3:
    funCall(*check)

Now, in funCall, ans will be storing the first element in check

Solution 2:

You misunderstood how *args works. Args is a tuple and contain all not-named arguments passed in to the method.

For example imagine this:

deffoo(*args):
    print(args)

foo([1,2,3], True, 1) # Prints ([1,2,3], True, 1,)
foo(*[1,2,3]) # Prints (1,2,3,)

Args is a tuple that contains all of the argument passed in. So in your case args[0] is just the first thing passed in, not the first index of your list.

If you want to populate the tuple with your item values you can pass it in prefixed with a * like this foo(*[1, 2, 3,]). That will print out (1, 2, 3,) and your index 0 will be the first item of the list you passed in.

Post a Comment for "How Do I Index A List Contained Inside *args?"