I need help to print this python program

I have a print output in list form and I want it converted to dictionary with the key: Name, Age, Course.

This is the output:
student_list = [[‘John’, 20, ‘Python’], [‘Bob’, 25, ‘Java’], [‘Dammy’, 22, ‘JS’]]

Question:

How can these values in the list be converted to dictionary so that each can be assigned to key: Name, Age, Course??

Thanks

my_dictionary = {name:[],age:[],course:[]}
for i in student_list:
  my_dictionary.name.append(i[0])
  my_dictionary.age.append(i[1])
  my_dictionary.course.append(i[2])

Is this what you meant?

1 Like

This method returning an error.
‘dict’ object has no attribute ‘name’

student_list = [[‘John’, 20, ‘Python’], [‘Bob’, 25, ‘Java’], [‘Dammy’, 22, ‘JS’]]

my_dictionary = {‘name’:, ‘age’:, ‘course’:}

for student in student_list:

my_dictionary['name'].append(student[0])

my_dictionary['age'].append(student[1])

my_dictionary['course'].append(student[2])

Something like this?

Also, when you have something repeating, making it into a function is a good idea. For this case I think even making a class is a good idea.

If you want the student’s name to be the key in the dictionary, you can change the code to something like:

my_dictionary = {}

for student in student_list:

my_dictionary[student[0]] = [student[1], student[2]]

Change this code and you can get what you want actually.

classroom = {'student1' : student1,
                             'student2' : student2,
                            'student3' : student3
                           }

You forgot to close the string for the keys.

The first method gave me what I wanted.

Thanks for your time.

Really appreciate!

glad to be of help :slight_smile:

This my below code is running fine but I want to go 2 steps further but becoming very difficult.

student_details = []
space = int(input('Please enter the number of student to be added: '))

for n in range(space):
                data = [
                input('enter your name: '),
                input('enter your age: '),
                input('enter your course: ')
                ]
                student_details.append(data)
                print(student_details)

print('{:<12} (:<12} {:<12}'.format('NAME', 'AGE', 'COURSE'))

details1 = {i : (student_details[i] for i in range(5)}

for key, value in details1.items():
          name, age, course = value
          print('{:<12} (:<12} {:<12}'.format(name, age, course))

While the code gives me result, I’m finding it difficult to do the following:

If I set the space to range(5), it means that I will have to input name, age and course for 5times, how can I use break statement to stop the process EVEN if i did not enter the value 5times without changing the range from the code and then still print whatever that is input maybe at range(3)?

Thanks

I think you should use while loop

add = 1;
while(add):
    # Input data as per above for loop
    add = int(input("Add more student?(1/0)"))

As far as you not enter add value as 0, it’ll ask you for student details…
As soon as you enter it as 0, it’ll break out of loop