superunknown
superunknown

Reputation: 117

pop in dictionary of list python

I have this python dictionary, I want to pop index0 element 'juan' from the unique key on the dict 'Resultados'.

data1 = {'Resultados':['juan','jose','carlos','pepe','ronny']}

How could I do?

I am using

data1.pop('Resultados'[0])

but is not working.

Upvotes: 0

Views: 73

Answers (2)

hpaulj
hpaulj

Reputation: 231605

it is not working is not how you should describe the problem. Show the actual error, and better yet, trying to understand it.

In [451]: data1.pop('Resultados'[0])
Traceback (most recent call last):
  File "<ipython-input-451-b2afd8c8d64b>", line 1, in <module>
    data1.pop('Resultados'[0])
KeyError: 'R'

'Resultados'[0] is the first letter of the string, 'R'. That isn't a valid key for that dict. Right?

Correct indexing:

In [456]: data1['Resultados']
Out[456]: ['juan', 'jose', 'carlos', 'pepe', 'ronny']
In [457]: data1['Resultados'][0]
Out[457]: 'juan'

or it you want to remove it from the dict value

In [459]: data1['Resultados'].pop(0)
Out[459]: 'juan'
In [460]: data1
Out[460]: {'Resultados': ['jose', 'carlos', 'pepe', 'ronny']}

When things don't work as expected, break the problem down into pieces, keeping in mind the order in which Python evaluates expressions. That evaluation order should be one of the first things you learn about the language.

Upvotes: 0

Joran Beasley
Joran Beasley

Reputation: 114038

data1['Resultados'].pop(0)

I guess?

Upvotes: 2

Related Questions