Sayantan Ghosh
Sayantan Ghosh

Reputation: 338

How to create an object from a list of lists?

I have a list of elements in the following manner:

lst = [['Adam','Class 1','English'],['Will','Class 3','Physics'],['George','Class 1','Maths'],['Sofia','Class 2','Chemistry']]

Now, I want to convert each of the inner lists into the following object.

class Student:
def __init__(self, name, class, subject):
self.name = name
self.class = class
self.subject = subject

I am trying the following command to create objects:

student_objs = {(name, class, subject): Student(student[0],student[1],student[2]) for student in lst}

However, this is giving an error. Suggestions please.

Upvotes: 0

Views: 998

Answers (3)

prajwal k Hebbar
prajwal k Hebbar

Reputation: 114

you cant use the class as an variable name in your Student class as it is a keyword.

The below code does the job for you. But it creates a set rather than obj.

lst = [['Adam','Class 1','English'],['Will','Class 3','Physics'],['George','Class 1','Maths'],['Sofia','Class 2','Chemistry']]

class Student:
    def __init__(self, name, _class, subject):
        self.name = name
        self._class = _class
        self.subject = subject

student_objs = {Student(*student) for student in lst}
for obj in student_objs:
    print("name:",obj.name,"\nclass:",obj._class,"\nsubject:",obj.subject)
    print("*"*20)

Output:

name: Sofia 
class: Class 2 
subject: Chemistry
********************
name: Will 
class: Class 3 
subject: Physics
********************
name: George 
class: Class 1 
subject: Maths
********************
name: Adam 
class: Class 1 
subject: English
********************

Upvotes: 2

campovski
campovski

Reputation: 3163

The biggest problem with your approach is that you are trying to use undeclared variables (name, class, subject). These are defined only in class Student. What you want to do is (subject[0], subject[1], subject[2]) instead, or simply use Pythons built-in function tuple(subject).

Another important thing is that you cannot use class as variable name! This is reserved keyword in Python. Instead, use className for example. So like this:

class Student:
    def __init__(self, name, className, subject):
        self.name = name
        self.className = className
        self.subject = subject

You cannot create and object from "list comprehension". What you can do is

student_objs = [Student(student[0], student[1], student[2]) for student in lst]

and have array of students.

Upvotes: 2

Sayandip Dutta
Sayandip Dutta

Reputation: 15872

One issue is class is a reserved keyword in Python, and trying to use that in list comprehension would not be a good idea, and may lead to errors. Name it something else. And then you can do:

lst = [['Adam','Class 1','English'],['Will','Class 3','Physics'],['George','Class 1','Maths'],['Sofia','Class 2','Chemistry']]
class Student:
    def __init__(self, name, Class, subject):
        self.name = name
        self.Class = Class
        self.subject = subject
student_objs = {tuple(student): Student(*student) for student in lst}
print(student_objs)

Output:

{('Adam', 'Class 1', 'English'): <__main__.Student at 0x9bee668>,
 ('Will', 'Class 3', 'Physics'): <__main__.Student at 0x9bee390>,
 ('George', 'Class 1', 'Maths'): <__main__.Student at 0x9beef28>,
 ('Sofia', 'Class 2', 'Chemistry'): <__main__.Student at 0x9beeda0>}

Also, your original code would have worked if you had named class as something else, and did this,

student_objs = {(name, Class, subject): Student(name, Class, subject) for name, Class, subject in lst}

Upvotes: 2

Related Questions