473,698 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Moving items from list to list


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

Jun 14 '07 #1
4 14228
On 6/14/07, HMS Surprise <jo**@datavoice int.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.co m>
Jun 14 '07 #2
Thanks.

That will work. The 2nd, smaller lst starts out empty but this is
easily adapted.

jh

Jun 14 '07 #3
On Jun 14, 12:30 pm, HMS Surprise <j...@datavoice int.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.po p(4))
print x, y
[0, 1, 2, 3, 5, 6, 7, 8, 9] [4]
>>y.append(x.po p(7))
print x, y
[0, 1, 2, 3, 5, 6, 7, 9] [4, 8]
HTH,
George

Jun 14 '07 #4
En Thu, 14 Jun 2007 14:23:14 -0300, Evan Klitzke <ev**@yelp.come scribió:
On 6/14/07, HMS Surprise <jo**@datavoice int.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(100000 0);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(100000 0);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(100000 0);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(100000 0);b=range(1000 )"
"b.append(a[0]
);a=a[1:]"
10 loops, best of 3: 110 msec per loop

--
Gabriel Genellina

Jun 14 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2396
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, one for each dragable item (boxes about 100x150px), inside another div that sets up a border. my script is instigated by an onMouseOver event and works great for moving items, setting the opacity, etc. The only trouble is (and I guess I should...
3
6261
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 "How to move items between Lists with Javascript" http://www.devx.com/GetHelpOn/10MinuteSolution/16372. My problem is that I also have a dropdown list on the page with autopost back = true, and a procedure to handle the text changed event of the...
5
11452
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 versa, this works fine. I also have a dropdown list on the page with autopost back = true, my problem is as follows: when the user selects an option in the dropdown list the form is posted back to the server and certain other processing happens, then...
1
1423
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
3
7389
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 Add button, and the selected items will move to the second ListBox. I've been trying to use the ListBox.SelectedObjectCollection with no success. It sounds like the logical thing to use but I can't seem to find
1
2403
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 in one box by dragging them to another and sort them in the latter, and it works a treat. However, I'd now like to add a couple of buttons, "<<" and ">>" between
0
1848
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 the country. allows you to easily obtain no obligation moving quotes from local movers, long distance movers, international movers, auto transport, storage rentals and specialty movers. office movers, commercial moving, residential moving, movers...
0
1779
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 the country. allows you to easily obtain no obligation moving quotes from local movers, long distance movers, international movers, auto transport, storage rentals and specialty movers. office movers, commercial moving, residential moving, movers...
3
5048
JodiPhillips
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 on a form. The first listbox is populated according to command buttons (Command 14 & Command 15) that are clicked by the user (draws data via SQL statement - see code below). The second listbox is populated by user selection from the first listbox. I...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.