Python Forum
what's homogeneus items defined by list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what's homogeneus items defined by list
#1
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!!!
Reply
#2
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.
Reply
#3
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
Reply
#4
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.
Reply
#5
get it! Thanks for your time, Gribouillis.
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  variable not defined inside a list comprehension Phidias618 1 1,844 May-13-2023, 09:05 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020