#๐ is this a dictionary
77 messages ยท Page 1 of 1 (latest)
Hey @spark temple!
It looks like you're trying to paste code into this channel.
Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
@spark temple
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.
collection = {"a": "snake", "b": "dog", "d": "ant"}
print("Ah, need to add one more")
collection["c"] = "cat"
print("The collection is:")
for key, value in collection.items():
print(f"{key} -> {value}"
not too familier with this
is this a list?
or is "a" a way i can output snake
dict.items gives you a special kind of object that is specific to this method.
Let's have a look.
!e py d = {} print(type(d.items()))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
<class 'dict_items'>
!e
d={"cat"}
print(type(d.items()))
:x: Your 3.12 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "/home/main.py", line 2, in <module>
003 | print(type(d.items()))
004 | ^^^^^^^
005 | AttributeError: 'set' object has no attribute 'items'
It's an iterable, so it's easy enough to reference the tuples in a for loop.
huh
I don't think it's subscriptable, however.
!e
collection = {"a": "snake", "b": "dog", "d": "ant"}
print("Ah, need to add one more")
collection["c"] = "cat"
print("The collection is:")
for key, value in collection.items():
print(f"{key} -> {value}"
:x: Your 3.12 eval job has completed with return code 1.
001 | File "/home/main.py", line 7
002 | print(f"{key} -> {value}"
003 | ^
004 | SyntaxError: '(' was never closed
!e
collection = {"a": "snake", "b": "dog", "d": "ant"}
print("Ah, need to add one more")
collection["c"] = "cat"
print("The collection is:")
for key, value in collection.items():
print(f"{key} -> {value}")
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | Ah, need to add one more
002 | The collection is:
003 | a -> snake
004 | b -> dog
005 | d -> ant
006 | c -> cat
what is ur issue ?
Dictionaries and sets share the curly braces, as does some string formatting syntax.
not an issue just trying to learn how they work and what they are
that kinda makes sense
A dictionary is made of entries called items, each item is a key:value pairing.
maybe that can help you
You generally use dictionaries to refer to the values by providing the associated key.
is it just an easier way to print out stuff?
like a = massive sentence for example
i'll give some of these a watch thank you
Keys of a dictionary are unique as keys of that dictionary. No repeats.
the one from bro code
is pretty well explained
You know how we have variables, variables being named references to objects. Dictionaries are like that, but contained in an object.
can dictionaries be changed?
Dictionaries are mutable, yes.
You can change the data within and still have the same object.
ahh alright so they're just kinda boxes of variables
A dictionary is like a box of variables pointing to objects, yes.
that makes so much sense thank you
watching it now its really good
thanks for the help
The variables are the keys, the objects are the values, together each pairing being an item.
so the reason it prints out the key and value here is because its telling it to print out item?
collection.items
!e py d = {'key a': 'value a', 'key b': 'value b', 'key c': 'value c'} print(d.keys()) print(d.values()) print(d.items())
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | dict_keys(['key a', 'key b', 'key c'])
002 | dict_values(['value a', 'value b', 'value c'])
003 | dict_items([('key a', 'value a'), ('key b', 'value b'), ('key c', 'value c')])
thats would have been so helpful in some past projects
You're also employing variable unpacking, which is often useful here.
variable unpacking?
!e py key, value = ('key a', 'value a') print(key) print(value)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | key a
002 | value a
!e py d = {'key a': 'value a', 'key b': 'value b', 'key c': 'value c'} for kv in d.items(): print(kv)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | ('key a', 'value a')
002 | ('key b', 'value b')
003 | ('key c', 'value c')
!e py d = {'key a': 'value a', 'key b': 'value b', 'key c': 'value c'} for key, value in d.items(): print(f'{key = }, {value = }')
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | key = 'key a', value = 'value a'
002 | key = 'key b', value = 'value b'
003 | key = 'key c', value = 'value c'
oh i've never seen a f' string like that before
!e py a, b, c = 1, 2, 3 print(a) print(b) print(c)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 1
002 | 2
003 | 3
!e
a, b, c = 1, 2, 3
print(a)
print(b)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 1
002 | 2
huh ok
!e py a, (b, c) = 1, (2, 3) print(a) print(b) print(c)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 1
002 | 2
003 | 3
thats good to understand gonna make future projects way easier to structure
thanks again man
Yeah, it's a little trick not everyone knows about.
Wait, the f-string or the {} notation?
notation
Mm.
It's just a way of displaying the information in the string.
huh i'll experiement around with these more when i get time to get learned experince
going back to assignment now
!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.