#๐ dictionary formatting
35 messages ยท Page 1 of 1 (latest)
@oblique mantle
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.
Can you share the dictionary?
unpacking would make this quite simple
!e
data = {'x': ['a', 'b', 'c']}
for k, v in data.items():
print(k, *v, sep=', ')
:white_check_mark: Your 3.12 eval job has completed with return code 0.
x, a, b, c
@oblique mantle still there?
>>> help(print)
Help on built-in function print in module builtins:
print(*args, sep=' ', end='\n', file=None, flush=False)
Prints the values to a stream, or to sys.stdout by default.
sep
string inserted between values, default a space.
end
string appended after the last value, default a newline.
file
a file-like object (stream); defaults to the current sys.stdout.
flush
whether to forcibly flush the stream.
thank you! it works well
another question, what does *v do?
* before an iterable will unpack its contents into arguments
since print accepts unlimited arguments, it will place each item as a separate argument
data = ['a', 'b', 'c']
print(*data)
# Is basically the same as
print(data[0], data[1], data[2])
these are basically the same
okay cool that makes sense
but with *data, it doesn't matter how many items there are inside the list
no, because that's still a list
Look at my example here
isn't that a list as well?
data[0] is not a list
it's a string
"unpacking" is taking the elements of the list one at a time and passing them as arguments
!e
data = ['a', 'b', 'c']
print(data)
print(*data)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | ['a', 'b', 'c']
002 | a b c
look at the difference
that makes sense. thank you for the help!
Np! If that's all for now, you can close this thread with !close
!close
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.