0

This is the screenshot of the code in question, on Chegg.com

CheggStudy Q&A

and this is the code itself, copypasted from Chegg.com

contact_emails = {
    'Sue Reyn': 's.reyn@email.com',
    'Mike Filt': 'mike.filt@bmail.com',
    'Nate Arty': 'narty042@nmail.com'
}

new_contact = input()
new_email = input()
contact_emails[new_contact] = new_email

for k, v in contact_emails.items():
    print(v, "is", k)

My screen looked like this: My Screen With Code Added

...As you can see, it works flawlessly despite it missing some of the code included in the suggested answer, and I just don't know how it works in general. I was glad for the help but I did not get any more information about how and why it works! Thanks in advance!

1 Answers1

1

.items() will return a view object. This object contains a dynamic view of all your key-value pairs in the dictionary as tuples, updating as the dictionary updates. The for loop then calls the iter function on the view, which returns an iterator. The iterator represents all your key-value pairs as a stream of data. Again, behind the scenes, the for loop will grab each successive key-value pair using the __next__() method. The __next__() method returns a tuple with your key and value. Finally, you use tuple unpacking to split this tuple into 2 different variables. Here is a bit what this code would look like behind the scenes without a for-loop.

contact_emails = {
    'Sue Reyn': 's.reyn@email.com',
    'Mike Filt': 'mike.filt@bmail.com',
    'Nate Arty': 'narty042@nmail.com'
}

# you can modify your dict if you want here

# get our view of the dict
items = contact_emails.items() 

# we get our stream of key-value pairs
# the for loop will do this in the background
item_iterator = iter(items) 

try:
    # we use while True because we use an error to exit the loop
    while True:
        # we call __next__() on the iterator to get our next pair
        kv_pair = item_iterator.__next__()

        # we assign iteration variables here
        # this special syntax is called "tuple unpacking"
        # It lets us split a pair of data into 2 variables easily.
        k, v = kv_pair

        # this is what would be the body of the for-loop
        print(v, "is", k)
        # ... 

# the __next__() raises an exception to break out of the while loop.
# it can't use the break keyword because it is inside a method and doesn't know about the while loop
except StopIteration:
    # we've accomplished our goal of leaving the while loop
    # we can just ignore the error and continue moving on
    pass

This syntax is kind of bulky, so python will instead just perform all the magic for us inside the for-loop construct.

Here's some further reading you can do about iterators, exceptions, tuple unpacking, and for loops.

Python iter() method - GeeksForGeeks

Iterables - Python Like You Mean It

__next__ in generators and iterators and what is a method-wrapper? - StackOverflow

Are exceptions for flow control best practice in Python? - Software Engineering StackExchange

Unpacking a Tuple in Python - GeeksForGeeks

Loop Better: a deeper look at iteration in Python - Trey Hunner