#๐Ÿ”’ def with Lists

13 messages ยท Page 1 of 1 (latest)

viral ivy
#

Hi guys is there any way to use the functions with the list so like instead of saying def random(x,y) instead of x,y you put a List??

spice cloudBOT
#

@viral ivy

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

sour torrent
#

In sounds like you're asking about unpacking

#

!e

def func(x, y):
    print(f'{x=}, {y=}')

lst = [1, 2]
func(*lst)
spice cloudBOT
sour torrent
#

* "spreads" the iterable into the argument list.

#

Of course though, you need to be careful to ensure that the length of the list matches the number of parameters the function has, or you'll get an error.

sour torrent
#

You're welcome.

heavy carbon
#

You can also pass in a list and operate on that from within the function as well

wanton bluff
#

The flip side of this is a function which accepts a variable number of arguments:

def func(*args):
    print(len(args), args)

Usually we prefer the normal function definition with fixed names parameters, but sometimes the variable form is appropriate.

spice cloudBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.