Dear All,
If I have a list, say
x=[1,2,3,4,5,6]
What's the best way of converting it into this: [[1, 2], [3, 4], [5, 6]],
i.e. splitting it into pairs.
Many thanks
Colin 6 1987
C Gillespie wrote: Dear All,
If I have a list, say
x=[1,2,3,4,5,6]
What's the best way of converting it into this: [[1, 2], [3, 4], [5, 6]], i.e. splitting it into pairs.
There are a variety of solutions in the Python Cookbook
here http://aspn.activestate.com/ASPN/Coo...kwd=Algorithms
I notice "Getting items in batches", and "Group a list into
sequential n-tuples" right off the bat.
-Peter
C Gillespie wrote: Dear All,
If I have a list, say
x=[1,2,3,4,5,6]
What's the best way of converting it into this: [[1, 2], [3, 4], [5, 6]], i.e. splitting it into pairs.
don't know if it's the best way but you can try this:
pairs = [x[2*i:2*i+2] for i in xrange(len(x)/2)]
> If I have a list, say x=[1,2,3,4,5,6]
What's the best way of converting it into this: [[1, 2], [3, 4], [5, 6]], i.e. splitting it into pairs.
I like x = [1, 2, 3, 4, 5, 6] it = iter(x) zip(it, it)
[(1, 2), (3, 4), (5, 6)]
even after posting it several times before :-) Use map(list, zip(it, it)) if
you need lists instead of tuples.
Michele Simionato has posted a generalized version of this recipe: http://mail.python.org/pipermail/pyt...ay/222673.html
Peter
C Gillespie wrote: Dear All,
If I have a list, say
x=[1,2,3,4,5,6]
What's the best way of converting it into this: [[1, 2], [3, 4], [5, 6]], i.e. splitting it into pairs.
Many thanks
Colin
I would use:
pairs=[[a,b] for a,b in x]
but as you can see from other replies there are other ways.
Note: this wouldn't work if there were not an EVEN number
of data items in the list.
Larry Bates
Syscon, Inc.
Larry Bates wrote: x=[1,2,3,4,5,6]
pairs=[[a,b] for a,b in x] [[a,b] for a,b in x]
TypeError: unpack non-sequence
Larry Bates <lb****@syscononline.com> wrote:
... x=[1,2,3,4,5,6]
What's the best way of converting it into this: [[1, 2], [3, 4], [5, 6]],
... I would use:
pairs=[[a,b] for a,b in x]
but as you can see from other replies there are other ways.
Which is fortunate, because what you would use could never possibly work
for the value of x given by the original poster (and most other values
of x except sequences of two-items subsequences...).
Alex This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bart Nessux |
last post by:
I understand how a Python list can be broken into smaller lists by using
slice like this:
new_small_list_1 = BIG_LIST #0-1000
new_small_list_2 = BIG_LIST #1001-2000
new_small_list_3 =...
|
by: MarionEll |
last post by:
XML Conference 2003 program now available…register with discounts now!
IDEAlliance has just released the full program for XML 2003 scheduled December
7-12 at the Philadelphia Convention Center....
|
by: Chris Sharman |
last post by:
Writing a form, several questions have got a list of checkboxes, with
associated descriptions (mostly one word), all on a line.
Eg:
Describe yourself: Fat Hairy Ugly Wears glasses.
I want to...
|
by: Frances Del Rio |
last post by:
is there a non-breaking hyphen in HTML?? for example, so a phone no.
falls all on one line.. as in..
1-800-444-5454... (and is not broken into two lines if phone no. occurs
near end of a...
|
by: The Bicycling Guitarist |
last post by:
Hi. I found the following when trying to learn if there is such a thing as a
non-breaking hyphen. Apparently Unicode has a ‑ but that is not
well-supported, especially in older browsers. Somebody...
|
by: stevenkobes |
last post by:
If a word has a hyphen in it, IE will permit a line break at the
hyphen, but Firefox/Mozilla won't. Apparently the Firefox behavior is
standards-compliant, but it is not what I want. Is there a...
|
by: John Wood |
last post by:
I saw that Microsoft have released a list of breaking changes in .Net here:
http://msdn.microsoft.com/netframework/programming/breakingchanges/runtime/default.aspx
While this is useful, it seems...
|
by: tony |
last post by:
If you have any PHP scripts which will not work in the current releases
due to breaks in backwards compatibility then take a look at
http://www.tonymarston.net/php-mysql/bc-is-everything.html and...
|
by: elmar |
last post by:
Hi Clers,
If I look at my ~200000 lines of C code programmed over the past 15
years, there is one annoying thing in this smart language, which
somehow reduces the 'beauty' of the source code...
|
by: Nehil |
last post by:
i've following union
union header
{
struct
{
unsigned int size;
union header *next;
}s;
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |