1

I'm trying to create a dictionary of the form:

{: [ , {}]}

For example:

d = {term: [number, {number1: number2}]}

I tried to create the dictionary inside but I'm new and I couldn't understand how it's possible. The problem is that I want the form of d and I want to update number or the dictionary that contains number1 as key and number2 as value when finding term.

So the question is: Is it possible to create a dictionary like d ? And if so, how can I access term, number and the inside dictionay?

2 Answers 2

1
d = {"term": [5, {6: 7}]}

The key's value is a list:

d["term"]   
[5, {6: 7}]

The list 's 1st element:

d["term"][0]
5

The list 's second element is a dictionary:

d["term"][1]
{6: 7}

The value of the dictionary's key '6' is 7:

d["term"][1][6]
7

Edit: Some examples for modification:

d = {"term": [5, {6: 7}]}

d["term"].append(10)
print(d)
Out: {'term': [5, {6: 7}, 10]}

l=d["term"]
l[0]=55
print(d)
Out: {'term': [55, {6: 7}, 10]}

insidedict=l[1]
print(insidedict)
{6: 7}

insidedict[66]=77
print(d)
{'term': [55, {6: 7, 66: 77}, 10]}
Sign up to request clarification or add additional context in comments.

1 Comment

If I have multiple keys ,say term , term1 , termk how can I add values in the inside dictionary of term or update term's numbers or update term's inside dictionary ?
1

Sure, just define it as you have:

d = {'term': [5, {6: 7}]}

Since your dictionary has just one key, you an access the key via:

key = next(iter(d))

You can then access the value 5 via a couple of ways:

number = d[key][0]
number = next(iter(d.values()))[0]

Similarly, you can access the inner dictionary via either:

inner_dict = d[key][1]
inner_dict = next(iter(d.values()))[1]

And repeat the process for inner_dict if you want to access its key / value.

5 Comments

First, I would like to create an empty dictionary of the form {: [ , {}]} . Can you show me an example ?
@Devilhorn, Just instantiate it with d = {'': [0, {0: 0}]}, though I'm not sure why this is helpful. Python isn't strongly typed, you don't need to define in advance the type of every key and value.
Thank's @jpp , I instantiated it outside the loop I'm using it with the first values I need and it worked perfectly. As for the empty dictionary I was curious to find a way for educational purposes..
I have another problem, how can I get only the keys of the inside dictionary. For instance: say inside dictionary is :{name1:5,name2:2,name3:7} I need to check if name4 is in the inside dictionary as key , if not add it with empty value . So the dictionary in that case would be: inside dict is: {name1:5,name2:2,name3:7,name4:''} else if name4 is in dictionary it would remain the same.
I found that if I reach inside dict with: d[term][1].keys() I get all the keys but I don't know how to continue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.