473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Best idiom to unpack a variable-size sequence

ram
Here's a little issue I run into more than I like: I often need to
unpack a sequence that may be too short or too long into a fixed-size
set of items:

a, b, c = seq # when seq = (1, 2, 3, 4, ...) or seq = (1, 2)

What I usually do is something like this:

a, b, c = (list(seq) + [None, None, None])[:3]

but that just feels rather ugly to me -- is there a good Pythonic
idiom for this?

Thx,
Rick
Dec 18 '07 #1
2 1905
On Dec 18, 12:49 pm, ram <rickmorri...@gmail.comwrote:
Here's a little issue I run into more than I like: I often need to
unpack a sequence that may be too short or too long into a fixed-size
set of items:

a, b, c = seq # when seq = (1, 2, 3, 4, ...) or seq = (1, 2)

What I usually do is something like this:

a, b, c = (list(seq) + [None, None, None])[:3]

but that just feels rather ugly to me -- is there a good Pythonic
idiom for this?
In terms of brevity I don't think so; however it can be done more
efficient and general (e.g. for infinite series) using itertools:

from itertools import islice, chain, repeat

def unpack(iterable, n, default=None):
return islice(chain(iterable,repeat(default)), n)

a, b, c = unpack(seq, 3)

George
Dec 18 '07 #2
ram <ri**********@gmail.comwrote:
Here's a little issue I run into more than I like: I often need to
unpack a sequence that may be too short or too long into a fixed-size
set of items:

a, b, c = seq # when seq = (1, 2, 3, 4, ...) or seq = (1, 2)

What I usually do is something like this:

a, b, c = (list(seq) + [None, None, None])[:3]

but that just feels rather ugly to me -- is there a good Pythonic
idiom for this?
Pythonic might be to be explicit: i.e. know in advance how long the
sequence actually is.

One drawback I see with your code is that it doesn't give you any way to
specify different defaults for the values. So here's an alternative to
consider: try passing your sequence to a function. This lets you specify
appropriate defaults, and it reads quite cleanly. Of course it also forces
you to extract the code using those variables out into a separate function,
but that may not be a bad thing.
>>def process(a=None, b=None, c=None):
print a, b, c

>>seq = iter('abcd')
process(*itertools.islice(seq,0,3))
a b c
>>seq = iter('ab')
process(*itertools.islice(seq,0,3))
a b None
>>>
Dec 18 '07 #3

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

Similar topics

5
by: Geoffrey | last post by:
Hope someone can help. I am trying to read data from a file binary file and then unpack the data into python variables. Some of the data is store like this; xbuffer:...
7
by: | last post by:
How to call a function with variable argument list from another function again with variable argument list? Example : double average ( int num, ... ); double AFunct1 ( int num, ... ); double...
0
by: ft_ml | last post by:
Hello all, I´m trying to unpack variable length strings. In an application I receive a stream of bytes which contains a number n (2 bytes) and n strings of variable length. All strings ends...
5
by: tmp123 | last post by:
Hello, Thanks for your time. After review the "struct" documentation, it seems there are no option to pack/unpack zero terminated strings. By example, if the packed data contains: byte +...
31
by: eliben | last post by:
Hello, In a Python program I'm writing I need to dynamically generate functions and store them in a dict. eval() can't work for me because a function definition is a statement and not an...
1
by: mh | last post by:
What's the best Python idiom for this C construct? while ((x = next()) != END) { .... } Now I'm doing x = next() while x != END:
0
by: Jean-Paul Calderone | last post by:
On Tue, 26 Aug 2008 16:42:30 GMT, mh@pixar.com wrote: Use the magical second parameter to the `iter´ builtin: for x in iter(next, END): ... Jean-Paul
17
by: JRough | last post by:
I have used this function to create a string called $headers: function GetHeaders($file_name){ return "<th><a href='".$file_name."&order_by=l_e'>L_E</a></th> <th><a...
19
by: JRough | last post by:
I have used this function to create a string called $headers: function GetHeaders($file_name){ return "<th><a href='".$file_name."&order_by=l_e'>L_E</a></th> <th><a href='"....
28
by: Trups | last post by:
HI, I want to dynamically allocate array variable of type LPWSTR. Code looks like this... main() { LPWSTR *wstr; int count = Foo (wstr); for (int i = 0; i < count; i++)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
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...
0
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...
0
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,...
0
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...
0
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,...
0
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...

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.