Just wondered if there was some python idiom for moving a few items
from one list to another. I often need to delete 2 or 3 items from one
list and put them in another. Delete doesn't seem to have a return
value. I don't care which items I get so now I just use a couple of
pops or a for loop for more than two.
Thanks
jh 4 14171
On 6/14/07, HMS Surprise <jo**@datavoiceint.comwrote:
>
Just wondered if there was some python idiom for moving a few items
from one list to another. I often need to delete 2 or 3 items from one
list and put them in another. Delete doesn't seem to have a return
value. I don't care which items I get so now I just use a couple of
pops or a for loop for more than two.
I'm not sure if this is what you're asking, but if the elements in the
list are contiguous you can just use list slicing/addition, like this:
a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
b = a[2:] + b
a = a[:2]
Now the contents of a and b respectively are a = [1, 2] and b = [3, 4,
5, 6, 7, 8, 9, 10].
--
Evan Klitzke <ev**@yelp.com>
Thanks.
That will work. The 2nd, smaller lst starts out empty but this is
easily adapted.
jh
On Jun 14, 12:30 pm, HMS Surprise <j...@datavoiceint.comwrote:
Just wondered if there was some python idiom for moving a few items
from one list to another. I often need to delete 2 or 3 items from one
list and put them in another. Delete doesn't seem to have a return
value. I don't care which items I get so now I just use a couple of
pops or a for loop for more than two.
Thanks
jh
>>x = range(10) y = []
>>y.append(x.pop(4)) print x, y
[0, 1, 2, 3, 5, 6, 7, 8, 9] [4]
>>y.append(x.pop(7)) print x, y
[0, 1, 2, 3, 5, 6, 7, 9] [4, 8]
HTH,
George
En Thu, 14 Jun 2007 14:23:14 -0300, Evan Klitzke <ev**@yelp.comescribió:
On 6/14/07, HMS Surprise <jo**@datavoiceint.comwrote:
>> Just wondered if there was some python idiom for moving a few items from one list to another. I often need to delete 2 or 3 items from one list and put them in another. Delete doesn't seem to have a return value. I don't care which items I get so now I just use a couple of pops or a for loop for more than two.
I'm not sure if this is what you're asking, but if the elements in the
list are contiguous you can just use list slicing/addition, like this:
a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
b = a[2:] + b
a = a[:2]
Now the contents of a and b respectively are a = [1, 2] and b = [3, 4,
5, 6, 7, 8, 9, 10].
When lists become large this is less convenient because it has to build
three intermediate lists. At least on my box, pop+append is 5 orders of
magnitude faster (but as shown on another posts, you should test on your
box to see what happens):
c:\temp>call python -m timeit -s "a=range(1000000);b=range(1000)"
"b.append(a.po
p())"
1000000 loops, best of 3: 1.35 usec per loop
c:\temp>call python -m timeit -s "a=range(1000000);b=range(1000)"
"b+=a[-1:];a=a
[:-1]"
10 loops, best of 3: 107 msec per loop
c:\temp>call python -m timeit -s "a=range(1000000);b=range(1000)"
"b.append(a[-1
]);a=a[:-1]"
10 loops, best of 3: 107 msec per loop
c:\temp>call python -m timeit -s "a=range(1000000);b=range(1000)"
"b.append(a[0]
);a=a[1:]"
10 loops, best of 3: 110 msec per loop
--
Gabriel Genellina This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Kevin Blount |
last post by:
I'm playing with dragable <DIV>s, being inspired by google.com/ig,
where you can move items on the page around and have items you move
over change position if necessary.
I have 3 div's setup,...
|
by: Kay |
last post by:
Hello,
I have two list boxes on my form, one initially displays blank, and through
javascript it is possible to move items from one box to another, this works
fine, I followed an article titled...
|
by: Kay |
last post by:
Hello,
I have two list boxes on my form, one initially displays with items and the
other displays blank, by clicking a button, it is possible to move items
from one box to another and vice a...
|
by: Partha Sarathy.G |
last post by:
I have code for moving the items from one list box to another listbox at client side
For more details mail to gps.mca@gmail.com
|
by: Dany P. Wu |
last post by:
Hi everyone,
One of my Windows forms contain two listbox controls, with Add and Remove
buttons between them.
The idea is to allow users to select multiple items from one ListBox, click
the...
|
by: OwlHoot |
last post by:
I am using Thomas Fuchs's amazing drag-and-drop JavaScript library
available at:
http://wiki.script.aculo.us/scriptaculous/show/DragAndDrop
to allow the user to select a subset of items listed...
|
by: linkswanted |
last post by:
We are your trusted source. World Moving & Storage is bonded and
licensed by the U.S. Department of Transportation and is one of the
largest residential moving and corporate relocation company in...
|
by: linkswanted |
last post by:
We are your trusted source. World Moving & Storage is bonded and
licensed by the U.S. Department of Transportation and is one of the
largest residential moving and corporate relocation company in...
|
by: JodiPhillips |
last post by:
Hello everyone, there are many questions and answers relating to moving items between two listboxes here and on the net in general, however, none answer my specific problem.
I have two listboxes...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: SueHopson |
last post by:
Hi All,
I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...
| |