#๐Ÿ”’ dict() behaving weirdly

20 messages ยท Page 1 of 1 (latest)

dusty estuary
#

so

my_dict = dict([('a', 1), ('b', 2)])
my_dict_2 = my_dict = dict([('a', 1)])```
these work just fine
```py
x = dict((['a', 1],['b', 2]))
y = dict((['a', 1]))```
but y here does not work. x does, however. All i changed was replaced replaced regular with square brackets. in other words at first put tuples in a list, worked. Put one tuple in a list, worked. Then put two lists in a tuple, worked. Then put one tuple in a list, does not work. Why is this?
vital edgeBOT
#

@dusty estuary

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.

unborn dawn
dusty estuary
unborn dawn
# dusty estuary but what's the problem with a redundant parenthesis? i just put a tuple inside a...

Maybe this will explain it better: ```py
x = dict( # Call dict parentheses
( # Tuple parentheses
['a', 1], # Tuple item
['b', 2] # Tuple item
) # Tuple parentheses
) # Call dict parentheses

y = dict( # Call dict parentheses
( # Redundant parentheses
['a', 1] # Call dict argument 1
) # Redundant parentheses
) # Call dict parentheses

Fixed

y = dict( # Call dict parentheses
( # Tuple parentheses
['a', 1], # Tuple item with trailing comma that tells python this is a tuple item, and not part of grouping parentheses
) # Tuple parentheses
) # Call dict parentheses

dusty estuary
#

also can you tell me if there's just a particular way to place arguments inside a function? Like can it be the case that dict() does not support lists inside of tuples or something like that which i need to know? Or can i just do whatever

dusty estuary
unborn dawn
#

What arguments a function accepts depends on the specifics of the function. You should read the docs of the function to figure out what it accepts https://docs.python.org/3/library/stdtypes.html#dict . In the case of dict, it can accept an iterable of two-length iterables, which a tuple of lists satisfies.

dusty estuary
#

ahh so every function has certain requirements of its own right?

unborn dawn
#

Yes

dusty estuary
#

when i create custom functions myself i can also set how many parameters it will accept not any more nor any less

unborn dawn
#

Yes. That is how functions operate by default

dusty estuary
#

and then what if my custom function can take in 3 parameters

#

and i give 3 parameters in the form of lets say 3 lists, but those lists also have other things inside. Would those be meaningless?

#

like would it show an error

unborn dawn
#

Python will throw an error if you give it the wrong number of arguments, but will not check that the arguments are of the right type. That is where static type checkers like mypy or pyright come in. They can use type hints on your function args to check that it is always called with the correct types. You can also manually make sure your arguments are the correct types inside the function using isinstance

dusty estuary
#

Oooo okay, i see functions are quite complicated

#

Thanks a lot!!

vital edgeBOT
#
Python help channel closed for inactivity

This help channel has been closed. 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.