Python Forum
what's homogeneus items defined by list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: what's homogeneus items defined by list (/thread-27877.html)



what's homogeneus items defined by list - frank0903 - Jun-25-2020

Quote:Lists are mutable sequences, typically used to store collections of homogeneous items
what's homogeneous items or homogeneous data?
as far as I know, a list object is able to store different type objects,I'm confused!!!


RE: what's homogeneus items defined by list - Gribouillis - Jun-25-2020

Python lists can store arbitrary kind of objects, but usually, one wants to do more than just storing the objects. Most of the time we want to apply the same procedures to all the objects and the applicable procedures depend on the type of the objects. That's why homogeneous collections are always preferable to inhomogeneous collections. This principle is not specific to the Python language, it is a general programming principle.


RE: what's homogeneus items defined by list - frank0903 - Jun-25-2020

Emmm,Thanks Gribouillis
yep, the same precedure to all the objects is better, in other words, more faster probably,right?
if handling inhomogeneous collections in a list, any side effect?
how to define homougeneous collections? I mean how to express it more exactly


RE: what's homogeneus items defined by list - Gribouillis - Jun-25-2020

frank0903 Wrote:how to define homougeneous collections? I mean how to express it more exactly
There is no general definition. Often it means that the objects are all instances of the same class. It really depends on what you want to do with the objects. For example if you want to be able to add an integer to each object, then it could be a definition of homogeneous collection: "a list of objects to which one can add an integer".

There is no side effect whatsoever, the list class doesn't care about the kind of objects that it contains. The only effect is that the code that uses this list will be simpler if it doesn't have to worry about the type of the objects.


RE: what's homogeneus items defined by list - frank0903 - Jun-25-2020

get it! Thanks for your time, Gribouillis.


RE: what's homogeneus items defined by list - ndc85430 - Jun-25-2020

If you have a list containing a heterogeneous sequence of items, then it means different positions in that list are going to have to be treated differently. Your code will then be littered with lots of checks on the index to handle those different cases. There are two problems with that:

1. It's hard to read. You end up wondering why those indices are what they are. I suppose you could have variables to give names to them, but you can't get away from the conditional checks. You'd be better off using a class or named tuple to model the structure.

2. It really won't scale very well. Today you might only have 5 items in that list, but what happens tomorrow when you have a 100 or more? That code is just going to be painful to maintain.