List index method for complex list item types?



I'm a Python newbie who just started learning the language a few weeks
ago. So these are beginner questions.

I have a list of sockets that I use for select.select calls like this:

ReadList,WriteList,EventList = select.select(self.SocketList,[],[],3)

In parallel with that list of sockets I want something that is like a
list of socket,PacketFragment pairs. I need this because I could do
recv and get part of a sent packet. I want to loop around and each time
add more to the fragment until I get a full packet which has an
internal byte structure of the types I'm expecting.

The idea is every time I do
self.SocketList.append(NewSocket)
also do
self.SocketPacketFragmentList.append((NewSocket,''))
so that the index into SocketList is the same as the index into
SocketPacketFragmentList. So I can use
self.SocketList.index(SomeSocket) find an item in SocketList and then
know how to find it in the other list.

MySocketIndex = self.SocketList.index(MyTargetSocket)

Then do:

MySubList = self.SocketPacketFragmentList[MySocketIndex]

Then

MyPacketFragment = MySubList[1]

But a thought struck me while writing this: Does Python not provide a
way to search a list of sublists to find something on, say, the value
of the first sublist item field as a way to find the index to the item
one wants in the parent list?

There does not appear to be an alternative to lists that has better
functionality for this purpose. Dictionaries are immutable, right? One
can't use dictionaries for doing look-ups on dynamically changing
lists?

For efficiency's sake it seems to me one wants a search function on
lists that returns two things:
- index where the item found.
- full item which matches on the particular field one is looking up
on.

Am I wrong in thinking Python doesn't really provide an automated way
to search lists of complex things?

Also, ii C++ one can use STL iterators to move thru a list or deque.
But if one needs to advance thru a list with array indexes that does
Python index in each time if one is looping thru the list?

In Python maybe the trick is to use
ii = 0
For item in List
# see if item matches

ii = ii + 1

and then somehow pop out of the for loop once one finds a match?

.



Relevant Pages

  • Re: Why are tuples immutable?
    ... but yet the fact that lists are mutable and tuples ... as dictionaries are one example. ... in the second we just allow mutables to ... > Except that Python uses dicts internally, ...
    (comp.lang.python)
  • No typical loops, and other crazy ideas
    ... manipulation capabilities of Python (training was largely the ... I've tried to manipulate the data just in Python ... types of data structures (dictionaries, sets, lists) and arguably 6 ... I haven't figured out the way of producing a list of dictionaries ...
    (comp.lang.python)
  • Re: Lists implemented as integer-hashed Dictionaries?
    ... Python lists are just dictionaries with lists hashed by integers. ... I looked at the source code for lists in python, ...
    (comp.lang.python)
  • PyCheck for a classes defined in python and user data in PyObject_HEAD
    ... in C I want to check if a given PyObject is a xml.dom.minidom.Node (or ... I'm in a situation when i don't really need to extend python with any ... The reason i'm asking is because while for dictionaries i can stick ... object in a key with an obscure name while in lists this would change ...
    (comp.lang.python)
  • Re: Non-web-based templating system
    ... I'm creating a small application in Python that uses lists and ... dictionaries to create a rudimentary database. ...
    (comp.lang.python)