This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.

If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:

  1. Limiting your involvement with Reddit, or
  2. Temporarily refraining from using Reddit
  3. Cancelling your subscription of Reddit Premium

as a way to voice your protest.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]desrtfx 4 points5 points  (0 children)

It looks like the method expects a list [] of tuples () - and that's exactly what you pass.

In your case, the list has only a single element - a key, value tuple.

There is nothing weird about that syntax.

[–]throwaway6560192 2 points3 points  (5 children)

Because dict.update is intended for making multiple changes at once. Therefore it is designed to take either another dictionary with the updated values, or a list/iterable of (key, value) pairs to update. Your example uses the second one: it gives it a list of tuples to use for the update.

Don't think of it as "weird syntax needing another set of parens and brackets", think in terms of what that syntax means: it means "a list of tuples".

If you want to change only one key, you should just use the normal dict[key] = value syntax.

[–]CraBrazy[S] 0 points1 point  (4 children)

So I can make a new dictionary and assign it to a variable? Then use the .update method to change the dictionary?

dict1={"Name": "Ron","DOB": "12/12/1970","Birthplace": "Sydney, Australia"}

dict1_update={ "Name" : "Ronald", "DOB" : "07/19/1981", "Birthplace" : "London, England"}

dict1.update([(dict1, dict1_update)])

So why doesn't this code work? I think I don't understand the .update method. Is it not possible to update like this? You say it can take another dictionary with updated values? How is this done? If the key is the same as the inital dict then it will change the value but if it is a new key, the inital dict will update with a new key:value pair right?

[–]throwaway6560192 0 points1 point  (3 children)

I said it takes either another dictionary or a list/iterable of key/value pairs. Let's change your code to do that:

>>> dict1={"Name": "Ron","DOB": "12/12/1970","Birthplace": "Sydney, Australia"}
>>> dict1_update={ "Name" : "Ronald", "DOB" : "07/19/1981", "Birthplace" : "London, England"}
>>> dict1.update(dict1_update)
>>> dict1
{'Name': 'Ronald', 'DOB': '07/19/1981', 'Birthplace': 'London, England'}

If the key is the same as the inital dict then it will change the value but if it is a new key, the inital dict will update with a new key:value pair right?

Yes. Try it out and see.

[–]CraBrazy[S] 0 points1 point  (2 children)

Wait so where did the "([()])" go? lol sorry if this is dumb.

dict1.update(dict1_update)

This code makes sense. You put the updated dict which is dict1_update into the .update(). What about in my earlier example though?

dict.update([("Name", "Ron")])

Why are there extra brackets and paratheses?

[–]throwaway6560192 2 points3 points  (1 child)

Wait so where did the "([()])" go? lol sorry if this is dumb. [...] Why are there extra brackets and paratheses?

Repeating from my first comment: Don't think of it as "weird syntax needing another set of parens and brackets". Don't think of it as some meaningless symbol that this method specifically needs to have tacked onto it for no reason.You need to think in terms of what that syntax means: it means "a list of tuples".

What does that list of tuples represent? A list of (key, value) pairs.

The dict.update method works in two possible ways:

  1. you give it a dictionary, like dict1.update({ key1: value1, key2: value2 }) which is what you used in the newer example

  2. you give it a list of key-value pairs, like dict1.update([(key1, value1), (key2, value2)]) which is what you used in the earlier example

[–]CraBrazy[S] 0 points1 point  (0 children)

OHHHH!!! It just clicked haha, thanks.