#๐ def with Lists
13 messages ยท Page 1 of 1 (latest)
@viral ivy
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.
In sounds like you're asking about unpacking
!e
def func(x, y):
print(f'{x=}, {y=}')
lst = [1, 2]
func(*lst)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
x=1, y=2
* "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.
ok thank you
You're welcome.
You can also pass in a list and operate on that from within the function as well
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.
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.