473,699 Members | 2,321 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

spliting a list by nth items

I feel like this has probably been answered before, but I couldn't
find something quite like it in the archives. Feel free to point me
somewhere if you know where this has already been answered.

I have a list in a particular order that I want to split into two
lists: the list of every nth item, and the list of remaining items.
It's important to maintain the original order in both lists.

So the first list is simple:

nth_items = items[::n]

The second list is what's giving me trouble. So far, the best I've
come up with is:

non_nth_items = list(items)
del non_nth_items[::n]

Is this the right way to do this?

Steve
--
You can wordify anything if you just verb it.
- Bucky Katt, Get Fuzzy
Jul 18 '05 #1
33 3009
Steven Bethard wrote:
I feel like this has probably been answered before, but I couldn't
find something quite like it in the archives. Feel free to point me
somewhere if you know where this has already been answered.

I have a list in a particular order that I want to split into two
lists: the list of every nth item, and the list of remaining items.
It's important to maintain the original order in both lists.

So the first list is simple:

nth_items = items[::n]

The second list is what's giving me trouble. So far, the best I've
come up with is:

non_nth_items = list(items)
del non_nth_items[::n]

Is this the right way to do this?


There's probably no "right" way. Your way isn't the way I
would have thought to do it, I think. But it's brilliant. :-)
(Also simple and clear.)

-Peter
Jul 18 '05 #2
Steven Bethard wrote:
I have a list in a particular order that I want to split into two
lists: the list of every nth item, and the list of remaining items.
It's important to maintain the original order in both lists.

So the first list is simple:

nth_items = items[::n]

The second list is what's giving me trouble.


non_nth_items = [x for x in items if x % n]
--
Michael Hoffman
Jul 18 '05 #3
I believe you meant somthing like:

non_nth_items = [items[i-1] for i in range(1,len(ite ms)-1) if i % n]

I don't think the "if x % n" won't work on his list of items

Larry Bates

"Michael Hoffman" <m.************ *************** ******@example. com> wrote in
message news:ci******** **@pegasus.csx. cam.ac.uk...
Steven Bethard wrote:
I have a list in a particular order that I want to split into two
lists: the list of every nth item, and the list of remaining items. It's
important to maintain the original order in both lists.

So the first list is simple:

nth_items = items[::n]

The second list is what's giving me trouble.


non_nth_items = [x for x in items if x % n]
--
Michael Hoffman

Jul 18 '05 #4
Larry Bates wrote:
I believe you meant somthing like:

non_nth_items = [items[i-1] for i in range(1,len(ite ms)-1) if i % n]

I don't think the "if x % n" won't work on his list of items


D'oh! I wasn't thinking--the test list of items I used was
range(something ), so of course it worked.

What I meant <wink> was:

non_nth_items = [x for index, x in enumerate(items ) if index % n]

Thanks for the correction.
--
Michael Hoffman
Jul 18 '05 #5
Larry Bates <lbates <at> swamisoft.com> writes:

I believe you meant somthing like:

non_nth_items = [items[i-1] for i in range(1,len(ite ms)-1) if i % n]


Yeah, the items list is not of the form range(x) -- in fact, my current use
for this is with a list of filenames -- so Michael Hoffman's solution:
non_nth_items = [x for x in items if x % n]


wouldn't work. In the full fledged problem, I actually have a start parameter
for the slice as well as the step parameter (n), so I could probably do
something along these lines (inspired by your two suggestions):

non_nth_items = [item for i, item in enumerate(items ) if (i - start) % n]

instead of my original

non_nth_items = list(items)
del non_nth_items[start::n]

The enumerate/range solution is a little more verbose, but it does only go
through the list once. Thanks for the good suggestions!

STeve
Jul 18 '05 #6
[Steven Bethard]
so I could probably do
something along these lines (inspired by your two suggestions):

non_nth_items = [item for i, item in enumerate(items ) if (i - start) % n]

instead of my original

non_nth_items = list(items)
del non_nth_items[start::n]

The enumerate/range solution is a little more verbose, but it does only go
through the list once. Thanks for the good suggestions!


The enumerate solution has a lot going for it. It is more flexible and easily
accomodates criteria other than every nth item. More importantly, it
demonstrates an important guiding principle: multiple list deletions are a code
smell indicating that building a new list is a better approach. On this
newsgroup, I've seen people tie themselves in knots with multiple in-place
deletions during iteration when the new list approach was clearer, more robust,
and faster.

The runtime behavior of the non_nth_items[start::n] approach is implementation
dependent. One can easily imagine a O(n**2) process running behind the scenes.
CPython is a smarter than that, but the code for list_ass_subscr ipt() in
listobject.c is not a pretty sight.
Raymond Hettinger
Jul 18 '05 #7
Raymond Hettinger writes:
The enumerate solution has a lot going for it. It is more flexible and
easily accomodates criteria other than every nth item.
Yeah, I'm not so much worried about this, or I wouldn't be using
items[start::n]
to make the nth_items list in the first place. (If I was planning to allow
for other criteria, I'd probably use filter or ifilter instead of the
slicing...)
More importantly, it demonstrates an important guiding principle: multiple
list deletions are a code smell indicating that building a new list is a
better approach.
This was kinda my feeling -- hence why I posted. Part of the problem for me
was that because the two lists were so closely related semantically, I was
looking for a syntax that was also closely related. Sometimes this is a good
intuition to have, sometimes not. ;)
The runtime behavior of the non_nth_items[start::n] approach is
implementation dependent.


Ahh, I didn't know that. Good thing to know...

Thanks,

Steve

Jul 18 '05 #8
>>> x = [0,1,2,3,4,5,6,7 ,8,9]
n = 3
y = [x[i] for i in range(len(x)) if i%n==0]
z = [x[i] for i in range(len(x)) if i%n<>0]
x [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] y [0, 3, 6, 9] z

[1, 2, 4, 5, 7, 8]
"Steven Bethard" <st************ @gmail.com> wrote in message
news:ma******** *************** *************** @python.org...
| I feel like this has probably been answered before, but I couldn't
| find something quite like it in the archives. Feel free to point me
| somewhere if you know where this has already been answered.
|
| I have a list in a particular order that I want to split into two
| lists: the list of every nth item, and the list of remaining items.
| It's important to maintain the original order in both lists.
|
| So the first list is simple:
|
| nth_items = items[::n]
|
| The second list is what's giving me trouble. So far, the best I've
| come up with is:
|
| non_nth_items = list(items)
| del non_nth_items[::n]
|
| Is this the right way to do this?
|
| Steve
| --
| You can wordify anything if you just verb it.
| - Bucky Katt, Get Fuzzy
Jul 18 '05 #9
I was one of the frequent posters on the many tools/ides/gui threads
that appeared here over the past weeks. I'm now partly satisfied. My
search for a good IDE that is usable in my lowend machine is still not
completed. But I found a good setup using a free editor and PythonWin
working together.

I'm using Crimson Editor, a small freeware editor. The most
interesting part is that it isn't the most featured of all editors
I've tried; it isn't as powerful as vi or emacs, but has one feature
that I wanted the most, that is a fairly simple project management
tool. It simply allows me to organize a list of files that I'm using
for a project on the left pane. I can click on a file to open it. It's
amazingly useful when you're dealing with "hot" files stored on
several different directories, and you don't have to remember where is
it located, or to manually walk the open file dialog looking for it.

I'm using PythonWin for testing, mostly because the Windows console is
so braindead, and because PythonWin help is quick well organized (it's
about the same that you find in the web, but a little bit more
convenient).

I'm also evaluating Wing IDE 2.0; I had a few issues with it that were
solved, but for some tasks I found it too heavy for my limited
resources. Anyway, I'm keeping an eye on it.

--
http://mail.python.org/mailman/listinfo/python-list

--
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: ca********@gmai l.com
mail: ca********@yaho o.com
Jul 18 '05 #10

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

Similar topics

3
7527
by: Simon | last post by:
Hi, I'm hoping you could show me examples of how a functional/declarative language could be used to consicely describe resticted subsets of elements. I'm looking for a 'specification' style definition so any ideas/input would be very welcome. Thanks for your time, Simon. --
9
1541
by: Sreejith S S Nair | last post by:
hi there, I have a panel control which contain more than 10 label controls. these label controls are added dynamically by user. In this application user can slit a control into not less than 10 different shares. (don't worry about this. If a user select a control to split into 5 or 10. i am simply creating 5 or 10 controls which have the size 5/10 or 10/10 of the
0
1332
by: Sreejith S S Nair | last post by:
hi, Setp one. i have one panel control in my form. I am spliting this panel control into two part say working region and holding region. This spliting is only logic spliting. That is i am dividing(logically spliting) panels few portion (region) for
2
12619
by: Yoshitha | last post by:
hi I have 2 drop down lists in my application.1st list ontains itmes like java,jsp,swings,vb.net etc.2nd list contains percentage i.e it conatains the items like 50,60,70,80,90,100. i will select any skill in 1st drop down list then i'll select % of this skill in the 2nd list box , based on the percentage i've selected in the 2nd list box it has to display 2 sets of drop down list boxes at run time one for selecting skill and
5
1284
by: LU | last post by:
VAR1 = 3/4 VAR2 = 2 I user can submit the values "3/4" or "2" (VAR1, VAR2 represent any numbers) when user submits "3/4" we assume 1sthalf/2ndhalf These values are saved into database as a row row1: 3/4 row2: 2 I have a datagrid that displays values just fine, but user wants to beable to click on the 3 or 4 when we have a "/" in the value.
0
7539
by: Brian Henry | last post by:
Since no one else knew how to do this I sat here all morning experimenting with this and this is what I came up with... Its an example of how to get a list of items back from a virtual mode list view in .NET 2.0 with multiple selection turned on... since the .NET documentation is EXTREAML vague on how to do this and offers no clue on what the virtual mode events do or how to use them... just thought this might help someone else out...
0
3238
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another way... here is my code on how I did it to help anyone starting out get an idea of how to use virtual mode in ..NET 2.0 Imports CrystalDecisions.CrystalReports.Engine Imports CrystalDecisions.Shared
11
1352
by: s99999999s2003 | last post by:
hi i have a file with xxx.xxx.xxx.xxx:yyy xxx.xxx.xxx.xxx:yyy xxx.xxx.xxx.xxx:yyy xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx:yyy
1
1764
by: Nightman | last post by:
Hi How to split an address field up, when the user has entered it in one field with each address line separated by carriage return. I like to have a button, which can be clicked to autofill the address lines to related controls/fields on the form. How do I do that? Regards
0
8703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8623
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
9185
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
9050
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...
0
7773
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6540
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
4636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.