Moving list entries from one list to another

Bengt Richter bokr at oz.net
Sat Jul 13 17:34:38 EDT 2002


On Sat, 13 Jul 2002 22:37:44 +0200, JB <jblazi at hotmail.com> wrote:

>Emile van Sebille wrote:
>
>> JB
>>> I have two lists <list1> and <list2>. The entries of
>>> these lists have the format (id,rest), where <id> is a
>>> natural number. The list are sorted, the key is <id>.
>> 
>> Your use of the term 'key' here is throwing me.  Lists
>> don't have keys,
>> dicts do.  Dicts aren't sorted.  ;-/
>
>The list entries are tuples. The first element of a tuple is 
>the sorting key. It is a natural number n, 0<=n<N, where N 
>is the number of all entries. Some of the entries are in 
>list1 and some of them in list2. I am working on a list 
>view widget. list1 contains "visible lines" and list2 the 
>invisible lines. The moving between the lists is brought 
>about by changes of a user defined filter.
>
What about keeping all your lines in a single list, and just
filtering for purposes of generating the list view window?

OTOH, if you do want two lists, what about making them both
able to contain the full list, and just use None as a placeholder
when moving an element from one to the other? If your id numbers
are just the indices on the full set, you don't even need to
store them as data, since you know the range. And if you do
want a copy of one of the lists without the None placeholders,
you can use the builtin filter function to get non-False-evaluating
elements like
   noNonesList =  filter(None, alist).
(note that you will lose null strings that way though).

Now you just need a fast way to check both lists and see whether
your predicate says to swap corrensponding elements, which will be
None and something or something and None.

Gotta go...

Regards,
Bengt Richter



More information about the Python-list mailing list