PProteus PProteus - 1 year ago 80
Python Question

Creating Objects in Set or List

Is there a way to create new objects and put them in a set or list in one step? For example:

GreatList= [ a = MyObject("a"),
b = MyObject("b"),
c = MyObject("c")]

Answer Source

If you want both the a, b, c variables and the list:

a, b, c = GreatList = [MyObject("a"), MyObject("b"), MyObject("c")]

Consider whether you actually need the individual variables, though. It'd likely be better to just make the list:

GreatList = [MyObject("a"), MyObject("b"), MyObject("c")]

Note that the a, b, c = GreatList = ... thing wouldn't work with a set, since it relies on the list having a fixed iteration order.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download