0

Hello guys (or women) I try to do an application but I am blocked : I don't how how to get information from a dictionary into a dictionary

I know how to get it, how to read it but I don't know how to read each verb!

Could you help me if you have any information could help me I will really grateful!!

list = { { "FIELD1": "\"A\"", "FIELD2": "\"B\"", "FIELD3": "\"C\"", "FIELD4": "\"D\"", "FIELD5": "" }, { "FIELD1": "\"Base verbale (to...)\"", "FIELD2": "\"Preterit\"", "FIELD3": "\"Participe passe\"", "FIELD4": "\"Traduction\"", "FIELD5": "" }, { "FIELD1": "\"arise\"", "FIELD2": "\"arose\"", "FIELD3": "\"arisen\"", "FIELD4": "\"survenir\"", "FIELD5": "" }, } 

and what I have done :

import json from verbes import * file = "verbes.py" my_file = open(file, "r").read() keys = list.keys() print(keys) first_object = list["FIELD1"] print(first_object) 

Thank you again for your help!

10
  • 2
    That list should be wrapped in brackets ([...]) instead of braces ({...}). Commented Feb 13, 2019 at 18:01
  • 2
    Is it a list or a dict, because you seem to be using those terms interchangeably but they are very different. Second, what is your desired output? That would help a lot, since your problem description is very unclear Commented Feb 13, 2019 at 18:02
  • 2
    The title says "list into a list", the question says "dictionary into a dictionary". Which is it really? Commented Feb 13, 2019 at 18:02
  • 1
    Don't use list as a variable name, it's the name of a built-in type. Commented Feb 13, 2019 at 18:04
  • 1
    Make it list of dicts Commented Feb 13, 2019 at 18:04

3 Answers 3

1

First, fix your verbe list, do not use 'list' as variable name as it's a preserved keywords in python, and the bracket to declare a list should be [], not {}, like this: verbes.py:

verbe_list = [ { "FIELD1": "\"A\"", "FIELD2": "\"B\"", "FIELD3": "\"C\"", "FIELD4": "\"D\"", "FIELD5": "" }... ] 

If you want to use the list of verbes.py in another python script, you can import as below. The nested for loop would print all key and value in verbe_list.

main.py:

from verbes import verbe_list for item in verbe_list: for k, v in item.items(): print(k, v) 

Update: This give you all values of 'FIELD1'.

first_object_of_all_dict = [item['FIELD1'] for item in verbe_list] 

Result:

['"A"', '"Base verbale (to...)"', '"arise"'] 

If you want the value of 'FIELD1' in first dict from verbe_list:

first_object_of_first_dict = verbe_list[0]['FIELD1'] 

Result:

"A" 
Sign up to request clarification or add additional context in comments.

Comments

0

To begin with, I believe you may be confusing lists and dicts. What you really want to be dealing with here is a list: [] made up of dicts: {}. Here I have shown what you could potentially do. Since the list keyword is already used in python, you shouldn't really be defining your own variables with it.

verbes_list = [ { "FIELD1": "\"A\"", "FIELD2": "\"B\"", "FIELD3": "\"C\"", "FIELD4": "\"D\"", "FIELD5": "" }, { "FIELD1": "\"Base verbale (to...)\"", "FIELD2": "\"Preterit\"", "FIELD3": "\"Participe passe\"", "FIELD4": "\"Traduction\"", "FIELD5": "" }, { "FIELD1": "\"arise\"", "FIELD2": "\"arose\"", "FIELD3": "\"arisen\"", "FIELD4": "\"survenir\"", "FIELD5": "" } ] 

Furthermore, looking at your second piece of code you really don't need to start reading the whole file once again, rather you can import the specific variables which you wish to read. Just analysing your original code, I gathered that you would like to print the keys and the objects in your list of dicts and so you would do so by running a for loop through your list to get those 3 large chunks. Next we run another for loop to get the key and objects in each of those 3 chunks, before finally printing.

import json from verbes import verbes_list for chunk in verbes_list: for key, object in chunk.items(): print(key, object) 

Hope this helps :)

Comments

0

Instead of saving a .py same verbes.json with

[ { "FIELD1": "\"A\"", "FIELD2": "\"B\"", "FIELD3": "\"C\"", }... ] 

And then use json

import json list = json.load(open("verbes.json")) for d in list: print(d.keys()) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.