#๐Ÿ”’ I'm not sure how to get a specific key in a dictionary

32 messages ยท Page 1 of 1 (latest)

proud forge
#

I have a dictionary that looks like this:
{key1: [value1, value2], key2: [value3, value4]}
where each key is added by a user and a value comes along with it.
last name is a variable containing the last added/modified key
i want to pop() the last element of the last modified key and i think it should look something like this:

key(last_name).values().pop(len(key(last_name).values()))

but of course it doesn't work because im not sure of the syntax, so i need a little help

lament ibexBOT
#

@proud forge

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.

lean mesa
#

What was wrong with the previous solution you were provided?

sand kernel
#

It is very easy.

last_name = 'key2'
# You can pop the last element of the list associated with 'key2' like this
user_dict[last_name].pop()
lament ibexBOT
#

Hey @sand kernel!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make 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.
lean mesa
#

Yeah. This is the code previously given.

proud forge
#

yeah but the issue is not resolved

lean mesa
#

You don't need another thread. The answer has not changed.
You just need to show the code, as asked previously ๐Ÿ™‚

proud forge
#

and the code stays the same:

dict = {key1: [value1, value2], key2: [value3, value4]}
last_name = input()
print(dict[last_name].pop())
#

Here's the error:
TypeError: unbound method dict.pop() needs an argument

lean mesa
#

That's great, but your example code here isn't complete. There are variables involved which makes your code incomplete, the use of input() without providing the input, and the use of a native python type dict aliased to something else.
So... what is the actual code we can help you with?

#

I'm only arguing because I want to help. You're just restraining yourself and me being able to help you ๐Ÿ™‚

proud forge
#

imo it useless for me to give you the whole code but here it is:

for url in urls: 
    get_browser()
    name = input('Category: ')

    if name == 'exit':
            print(dict[last_name].pop())
            save_data()
            break

    if name not in data:
        data[name] = []

    data[name].append(url)
    last_name = name

a url opens in a tab, then u type a label for it and it stores it like:
{label : [url1]}
then i opens another url and if the label is the same it puts it as a second value in the array
as i said im overcomplicating it but you asked for it

the essential part is that if i type exit i want it to start from the url one before (which ive done) but then it makes duplicates so i want to remove the last stored url

#

urls is a [] and data is {}

lean mesa
#

print(dict[last_name].pop())
what is dict?

proud forge
#

the alternative of dictionary because dictionary doesnt exist

#

you cant dictionary[key].pop()

lean mesa
#

Have you assigned dict to anything?

proud forge
#

no

#

its a class

lean mesa
#

If dict is the standard/default type, then this will not work.

proud forge
#

and what will?

lean mesa
#

You should be referencing the dictionary you want to access.

#

Like the example:

some_variable = {"key1": ["value1", "value2"], "key2": ["value3", "value4"]}
print(some_variable["key1"].pop())
#

ie reference the dictionary variable that holds the keys and values you want to access and pop.

#

You have provided incomplete code, but I assume this might be data variable.

proud forge
#

yeah thanks

#

it finally worked

#

!close

lament ibexBOT
#
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.