1

I have a dictionary (named dict1) that has the following structure:

dict1 = {
 'Classroom': [{
 'Subject': "Calculus",
 'Students': [
 {'Name': "Joe", 'Age': 12, 'Weight': 126, 'Gender': "Male"},
 {'Name': "Doug", 'Age': 13, 'Weight': 95, 'Gender': "Male"},
 {'Name': ..., 'Age': ..., 'Weight': ..., 'Gender': ...}
 ]
 }]
}

The list (which contains name, age, weight, and gender) within this dictionary is very long, and I wanted to parse the Name, Age, and Gender out, and append them onto another list like this:

mylist = [("Joe", 12, "Male"), ("Doug", 13, "Male"), ... ]

I tried seraching online and tinkered around with code such as this:

mylist = []
dict2 = dict1['Classroom'][0]['Students'][0]['Name']
mylist.append(dict2)

But what happens is that it only appends the first name (Joe). Furthermore, I'm not sure how to parse three items (name, age, and gender) at the same time. Does anyone have a way to do this without having to use libraries?

Gino Mempin
30.5k31 gold badges125 silver badges174 bronze badges
asked Jul 27, 2019 at 18:59
4
  • Where is the original dict file? Commented Jul 27, 2019 at 19:04
  • 1
    @Ṁữl·lɪgắnậcễơưṩᛗ Apologies, I updated it. The original dict is the first dictionary that I wrote on my post. Commented Jul 27, 2019 at 19:05
  • 1
    The dictionary 'dict' consists of just one parent key and the value is a list. Commented Jul 27, 2019 at 19:08
  • 1
    @Ṁữl·lɪgắnậcễơưṩᛗ Correct, but there are dictionaries within that list, which is why I tried doing the "dict['classoom'][0]" thing to specify what items in the lists I want. But it only prints one name for some reason... Commented Jul 27, 2019 at 19:11

2 Answers 2

2

Your dict1 contains one key, 'Classroom' whose value is a list with one entry. That entry, is also a dict which contains two keys. You want the 'Students' key as that contains the list of all the students. It is a list. Iterate through that list and group the fields into a tuple.

It is technically a list ['Students'] nested within a dictionary ['Classroom'][0] within a list ['Classroom'] within a dictionary dict.

The code will be

mylist = []
studentlist = dict1['Classroom'][0]['Students']
for s in studentlist:
 m = (s['Name'], s['Age'], s['Gender'])
 mylist.append(m)

For the test case

classroom = {'Classroom': 
 [{'Subject': "Calculus", 'Students': 
 [
 {'Name': "Joe", 'Age': 12, 'Weight': 126, 'Gender': "Male"}, 
 {'Name': "Doug", 'Age': 13, 'Weight': 95, 'Gender': "Male"},
 {'Name': "Omg", 'Age': 50, 'Weight': 99, 'Gender': "Female"}
 ]
 }]
}

you will end up with

[('Joe', 12, 'Male'), ('Doug', 13, 'Male'), ('Omg', 50, 'Female')]

F16Falcon
4052 silver badges12 bronze badges
answered Jul 27, 2019 at 19:11
Sign up to request clarification or add additional context in comments.

1 Comment

This works, thanks! I was also just now thinking of using "for s in studentlist", so good to know that I'm learning! :)
1
student_list = []
classroom_list = dict1['Classroom']
# this is the first iteration list
for classroom in classroom_list:
 student_list = classroom['Students']
 # this is the second iteration list
 for student in student_list:
 student_list.append(student['Name'], student['Age'], student['Gender'])
#student_list = [("Joe", 12, "Male"), ("Doug", 13, "Male"), ... ]
answered Jul 31, 2019 at 15:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.