#๐Ÿ”’ is this a dictionary

77 messages ยท Page 1 of 1 (latest)

spark temple
#

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}")

wise oceanBOT
#

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

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.

spark temple
#
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

finite smelt
#

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()))

wise oceanBOT
spark temple
#

!e
d={"cat"}
print(type(d.items()))

wise oceanBOT
# spark temple !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'
finite smelt
#

It's an iterable, so it's easy enough to reference the tuples in a for loop.

spark temple
#

huh

finite smelt
#

I don't think it's subscriptable, however.

pallid harbor
#

!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}"

wise oceanBOT
pallid harbor
#

!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}")

wise oceanBOT
pallid harbor
#

what is ur issue ?

finite smelt
#

Dictionaries and sets share the curly braces, as does some string formatting syntax.

spark temple
#

that kinda makes sense

finite smelt
#

A dictionary is made of entries called items, each item is a key:value pairing.

pallid harbor
#

maybe that can help you

finite smelt
#

You generally use dictionaries to refer to the values by providing the associated key.

spark temple
#

is it just an easier way to print out stuff?

like a = massive sentence for example

spark temple
finite smelt
#

Keys of a dictionary are unique as keys of that dictionary. No repeats.

pallid harbor
#

is pretty well explained

finite smelt
spark temple
#

can dictionaries be changed?

finite smelt
#

Dictionaries are mutable, yes.

#

You can change the data within and still have the same object.

spark temple
#

ahh alright so they're just kinda boxes of variables

finite smelt
#

A dictionary is like a box of variables pointing to objects, yes.

spark temple
#

that makes so much sense thank you

spark temple
#

thanks for the help

finite smelt
#

The variables are the keys, the objects are the values, together each pairing being an item.

spark temple
#

so the reason it prints out the key and value here is because its telling it to print out item?
collection.items

finite smelt
#

!e py d = {'key a': 'value a', 'key b': 'value b', 'key c': 'value c'} print(d.keys()) print(d.values()) print(d.items())

wise oceanBOT
spark temple
#

thats would have been so helpful in some past projects

finite smelt
#

You're also employing variable unpacking, which is often useful here.

spark temple
#

variable unpacking?

finite smelt
#

!e py key, value = ('key a', 'value a') print(key) print(value)

wise oceanBOT
finite smelt
#

!e py d = {'key a': 'value a', 'key b': 'value b', 'key c': 'value c'} for kv in d.items(): print(kv)

wise oceanBOT
finite smelt
#

!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 = }')

wise oceanBOT
spark temple
#

oh i've never seen a f' string like that before

finite smelt
#

!e py a, b, c = 1, 2, 3 print(a) print(b) print(c)

wise oceanBOT
spark temple
#

!e
a, b, c = 1, 2, 3
print(a)
print(b)

wise oceanBOT
spark temple
#

huh ok

finite smelt
#

!e py a, (b, c) = 1, (2, 3) print(a) print(b) print(c)

wise oceanBOT
spark temple
#

thats good to understand gonna make future projects way easier to structure

#

thanks again man

finite smelt
#

Wait, the f-string or the {} notation?

spark temple
#

notation

finite smelt
#

Mm.

spark temple
#

does it just work like a end= but as a prefix?

#

instead of a suffix

finite smelt
#

It's just a way of displaying the information in the string.

spark temple
#

huh i'll experiement around with these more when i get time to get learned experince

#

going back to assignment now

#

!close

wise oceanBOT
#
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.