#๐Ÿ”’ is there a cute way to preload a dictionary with keys and a default value?

17 messages ยท Page 1 of 1 (latest)

mint notch
#

besides list comp or other form of doing it manually

like dicto = dict(keys=*'abc', default_value=None) or something???

rustic chasmBOT
#

@mint notch

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.

sullen perch
#

like this?

#

!e

my_dict = dict.fromkeys('abc', None)
print(my_dict)
rustic chasmBOT
#

@sullen perch :x: Your 3.12 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 1, in <module>
003 |     my_dict = dict.fromkeys(*'abc', None)
004 |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
005 | TypeError: fromkeys expected at most 2 arguments, got 4
cosmic topaz
#

splat isn't needed

#

!e

my_dict = dict.fromkeys('abc', None)
print(my_dict)
rustic chasmBOT
#

@cosmic topaz :white_check_mark: Your 3.12 eval job has completed with return code 0.

{'a': None, 'b': None, 'c': None}
sullen perch
#

mb

mint notch
#

woooo thank!

#

thanks!!!

humble patio
#

List comp is better for mutable values like empty list

cosmic topaz
#

or even consider defaultdict

mint notch
#

bet

#

this was mostly just bothering me as a thing i thought u could do but couldn't remember how

rustic chasmBOT
#
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.