473,396 Members | 2,013 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,396 software developers and data experts.

why does this unpacking work

I'm a little confused, but I'm sure this is something trivial. I'm
confused about why this works:
>>t = (('hello', 'goodbye'),
('more', 'less'),
('something', 'nothing'),
('good', 'bad'))
>>t
(('hello', 'goodbye'), ('more', 'less'), ('something', 'nothing'),
('good', 'bad'))
>>for x in t:
print x
('hello', 'goodbye')
('more', 'less')
('something', 'nothing')
('good', 'bad')
>>for x,y in t:
print x,y
hello goodbye
more less
something nothing
good bad
>>>
I understand that t returns a single tuple that contains other tuples.
Then 'for x in t' returns the nested tuples themselves.

But what I don't understand is why you can use 'for x,y in t' when t
really only returns one thing. I see that this works, but I can't quite
conceptualize how. I thought 'for x,y in t' would only work if t
returned a two-tuple, which it doesn't.

What seems to be happening is that 'for x,y in t' is acting like:

for x in t:
for y,z in x:
#then it does it correctly

But if so, why is this? It doesn't seem like very intuitive behavior.

Thanks.
Oct 20 '06 #1
16 1315
It's just sequence unpacking. Did you know that this works?:

pair = ("California","San Francisco")
state, city = pair
print city
# 'San Francisco'
print state
# 'California'

John Salerno wrote:
I'm a little confused, but I'm sure this is something trivial. I'm
confused about why this works:
>>t = (('hello', 'goodbye'),
('more', 'less'),
('something', 'nothing'),
('good', 'bad'))
>>t
(('hello', 'goodbye'), ('more', 'less'), ('something', 'nothing'),
('good', 'bad'))
>>for x in t:
print x
('hello', 'goodbye')
('more', 'less')
('something', 'nothing')
('good', 'bad')
>>for x,y in t:
print x,y
hello goodbye
more less
something nothing
good bad
>>>

I understand that t returns a single tuple that contains other tuples.
Then 'for x in t' returns the nested tuples themselves.

But what I don't understand is why you can use 'for x,y in t' when t
really only returns one thing. I see that this works, but I can't quite
conceptualize how. I thought 'for x,y in t' would only work if t
returned a two-tuple, which it doesn't.

What seems to be happening is that 'for x,y in t' is acting like:

for x in t:
for y,z in x:
#then it does it correctly

But if so, why is this? It doesn't seem like very intuitive behavior.

Thanks.
Oct 20 '06 #2
On Fri, 2006-10-20 at 15:14, John Salerno wrote:
I'm a little confused, but I'm sure this is something trivial. I'm
confused about why this works:
>>t = (('hello', 'goodbye'),
('more', 'less'),
('something', 'nothing'),
('good', 'bad'))
>>t
(('hello', 'goodbye'), ('more', 'less'), ('something', 'nothing'),
('good', 'bad'))
>>for x in t:
print x
('hello', 'goodbye')
('more', 'less')
('something', 'nothing')
('good', 'bad')
>>for x,y in t:
print x,y
hello goodbye
more less
something nothing
good bad
>>>

I understand that t returns a single tuple that contains other tuples.
t doesn't "return" anything, t *is* a nested tuple.
Then 'for x in t' returns the nested tuples themselves.
It again doesn't "return" anything. It assigns each element of tuple t
to x, one by one, executing the loop body for each element.
But what I don't understand is why you can use 'for x,y in t' when t
really only returns one thing. I see that this works, but I can't quite
conceptualize how. I thought 'for x,y in t' would only work if t
returned a two-tuple, which it doesn't.
You're thinking of "x,y = t".
What seems to be happening is that 'for x,y in t' is acting like:

for x in t:
for y,z in x:
#then it does it correctly
No, it's actually behaving like

for x in t:
y,z = t
# do something with y and z

You seem to have difficulty distinguishing the concept of looping over a
tuple from the concept of unpacking a tuple. This difficulty is
compounded by the fact that, in your example above, you are looping over
a tuple of tuples and unpacking each inner tuple on the fly.

Hope this helps,

Carsten.
Oct 20 '06 #3
On Fri, 2006-10-20 at 15:37, Carsten Haese wrote:
for x in t:
y,z = t
# do something with y and z
Typo here, of course I mean y,z = x.

-Carsten
Oct 20 '06 #4

John Salerno wrote:
I'm a little confused, but I'm sure this is something trivial. I'm
confused about why this works:
>>t = (('hello', 'goodbye'),
('more', 'less'),
('something', 'nothing'),
('good', 'bad'))
>>t
(('hello', 'goodbye'), ('more', 'less'), ('something', 'nothing'),
('good', 'bad'))
>>for x in t:
print x
('hello', 'goodbye')
('more', 'less')
('something', 'nothing')
('good', 'bad')
>>for x,y in t:
print x,y
hello goodbye
more less
something nothing
good bad
>>>

I understand that t returns a single tuple that contains other tuples.
Then 'for x in t' returns the nested tuples themselves.

But what I don't understand is why you can use 'for x,y in t' when t
really only returns one thing. I see that this works, but I can't quite
conceptualize how. I thought 'for x,y in t' would only work if t
returned a two-tuple, which it doesn't.

What seems to be happening is that 'for x,y in t' is acting like:

for x in t:
for y,z in x:
#then it does it correctly

But if so, why is this? It doesn't seem like very intuitive behavior.
It makes perfect sense: in fact, you have kind of explained it
yourself!

Think of the for statement as returning the next element of some
sequence; in this case it's a tuple. Then on the left side, the
unpacking occurs. Using "for x in t", means that effectively no
unpackig occurs, so you get the tuple. However, since the in is
returning a tuple, using "for x,y in t", the tuple returned gets
unpacked.

Hope that helps.

Jon.

Oct 20 '06 #5
At Friday 20/10/2006 16:14, John Salerno wrote:
>I'm a little confused, but I'm sure this is something trivial. I'm
confused about why this works:
>>t = (('hello', 'goodbye'),
('more', 'less'),
('something', 'nothing'),
('good', 'bad'))
I understand that t returns a single tuple that contains other tuples.
Then 'for x in t' returns the nested tuples themselves.

But what I don't understand is why you can use 'for x,y in t' when t
really only returns one thing. I see that this works, but I can't quite
conceptualize how. I thought 'for x,y in t' would only work if t
returned a two-tuple, which it doesn't.
You can think of

for x in t:
whatever

as meaning "for each element contained in t, name it x and do whatever"

The other concept involved is unpacking:
>>w = (1,2,3)
x,y,z = w
x
1

When you say "for x,y in t:" there is an implicit unpacking, it means
"for each element contained in t, unpack it into x and y and do whatever"
>What seems to be happening is that 'for x,y in t' is acting like:

for x in t:
for y,z in x:
#then it does it correctly
No, it acts like:

for w in t:
x,y = w
...
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
Oct 20 '06 #6
jo********@gmail.com wrote:
It's just sequence unpacking. Did you know that this works?:

pair = ("California","San Francisco")
state, city = pair
print city
# 'San Francisco'
print state
# 'California'
Yes, I understand that. What confused me was if it had been written like
this:

pair = (("California","San Francisco"))
Oct 20 '06 #7
Carsten Haese wrote:
You seem to have difficulty distinguishing the concept of looping over a
tuple from the concept of unpacking a tuple.
I think you're right. It's starting to make more sense now. I think when
I saw:

for x,y in t

I was expecting 't' to be a two-tuple for it to work. Maybe writing it as:

for (x,y) in t

sort of helps to show that '(x,y)' is equivalent to one object in 't'.
That makes it look a little more cohesive in my mind, I guess, or helps
me to see it map out against 't' better.
Oct 20 '06 #8
In <%b****************@news.tufts.edu>, John Salerno wrote:
jo********@gmail.com wrote:
>It's just sequence unpacking. Did you know that this works?:

pair = ("California","San Francisco")
state, city = pair
print city
# 'San Francisco'
print state
# 'California'

Yes, I understand that. What confused me was if it had been written like
this:

pair = (("California","San Francisco"))
Uhm, you mean::

pair = (("California","San Francisco"),)

Note the extra comma to make that "a tuple in a tuple".

Ciao,
Marc 'BlackJack' Rintsch
Oct 20 '06 #9
At Friday 20/10/2006 17:29, John Salerno wrote:
>I was expecting 't' to be a two-tuple for it to work. Maybe writing it as:

for (x,y) in t

sort of helps to show that '(x,y)' is equivalent to one object in 't'.
That makes it look a little more cohesive in my mind, I guess, or helps
me to see it map out against 't' better.
Note that it's the *comma* in an expression list what creates a
tuple, *not* the parens. A similar rule applies on the target side of
an assignment:

x,y = (1,2)
(x,y) = [1,2]
[x,y] = 1,2

and all variations are all equivalent.
The left part of a for statement is like an assignment.
With this in mind, it's not surprise that

for (x,y) in t: pass
for x,y in t: pass

are exactly the same.

<http://docs.python.org/ref/exprlists.html>
<http://docs.python.org/ref/assignment.html>
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
Oct 20 '06 #10
Marc 'BlackJack' Rintsch wrote:
Uhm, you mean::

pair = (("California","San Francisco"),)

Note the extra comma to make that "a tuple in a tuple".

Ciao,
Marc 'BlackJack' Rintsch
You're right!
Oct 20 '06 #11
John Salerno wrote:
I'm a little confused, but I'm sure this is something trivial. I'm
confused about why this works:
>>t = (('hello', 'goodbye'),
('more', 'less'),
('something', 'nothing'),
('good', 'bad'))
>>t
(('hello', 'goodbye'), ('more', 'less'), ('something', 'nothing'),
('good', 'bad'))
>>for x in t:
print x
('hello', 'goodbye')
('more', 'less')
('something', 'nothing')
('good', 'bad')
>>for x,y in t:
print x,y
hello goodbye
more less
something nothing
good bad
>>>

I understand that t returns a single tuple that contains other tuples.
Then 'for x in t' returns the nested tuples themselves.

But what I don't understand is why you can use 'for x,y in t' when t
really only returns one thing. I see that this works, but I can't quite
conceptualize how. I thought 'for x,y in t' would only work if t
returned a two-tuple, which it doesn't.
Hi John,

Thats the point were you go astray.
iterating over t *does* produce a 2-tuple that can be unpacked
immediately.

maybe this will help, (notice that element is always one of the inner
tuples):
>>tpl = ((00,01), (10,11), (20,21))
for element in tpl:
.... print "tpl provides this when iterated over:", element
....
tpl provides this when iterated over: (0, 1)
tpl provides this when iterated over: (10, 11)
tpl provides this when iterated over: (20, 21)
>>for element in tpl:
.... print "tpl provides this when iterated over:", element
.... sub0, sub1 = element
.... print "each element unpacks to:", sub0,"and:", sub1
....
tpl provides this when iterated over: (0, 1)
each element unpacks to: 0 and: 1
tpl provides this when iterated over: (10, 11)
each element unpacks to: 10 and: 11
tpl provides this when iterated over: (20, 21)
each element unpacks to: 20 and: 21
>>for sub0, sub1 in tpl:
.... print "each element of tuple unpacked immediately to:",
sub0,"and:", sub1
....
each element of tuple unpacked immediately to: 0 and: 1
each element of tuple unpacked immediately to: 10 and: 11
each element of tuple unpacked immediately to: 20 and: 21
>>>

- Paddy.

Oct 21 '06 #12
Hi,
In answering Johns question I had to approach it from a standpoint of
originally not seeing that their could be a difficulty; railing myself
in; then re-reading Johns question and trying even harder to put myself
in his shoes.
What I am interested in is if John and others might just take time out
to critique the replies. I'm interested in what the group think makes a
good comp.lang.python reply: too short, too long; too cryptic, too
simplistic, too polite (is their such a thing), too nasty; too
self-effacing, too self-promoting; too long a sig ;-) , too anonymous
....

Paddy wrote:
John Salerno wrote:
I'm a little confused, but I'm sure this is something trivial. I'm
confused about why this works:
>>t = (('hello', 'goodbye'),
('more', 'less'),
('something', 'nothing'),
('good', 'bad'))
>>t
(('hello', 'goodbye'), ('more', 'less'), ('something', 'nothing'),
('good', 'bad'))
>>for x in t:
print x
('hello', 'goodbye')
('more', 'less')
('something', 'nothing')
('good', 'bad')
>>for x,y in t:
print x,y
hello goodbye
more less
something nothing
good bad
>>>
I understand that t returns a single tuple that contains other tuples.
Then 'for x in t' returns the nested tuples themselves.

But what I don't understand is why you can use 'for x,y in t' when t
really only returns one thing. I see that this works, but I can't quite
conceptualize how. I thought 'for x,y in t' would only work if t
returned a two-tuple, which it doesn't.

Hi John,

Thats the point were you go astray.
iterating over t *does* produce a 2-tuple that can be unpacked
immediately.

maybe this will help, (notice that element is always one of the inner
tuples):
>tpl = ((00,01), (10,11), (20,21))
for element in tpl:
... print "tpl provides this when iterated over:", element
...
tpl provides this when iterated over: (0, 1)
tpl provides this when iterated over: (10, 11)
tpl provides this when iterated over: (20, 21)
>for element in tpl:
... print "tpl provides this when iterated over:", element
... sub0, sub1 = element
... print "each element unpacks to:", sub0,"and:", sub1
...
tpl provides this when iterated over: (0, 1)
each element unpacks to: 0 and: 1
tpl provides this when iterated over: (10, 11)
each element unpacks to: 10 and: 11
tpl provides this when iterated over: (20, 21)
each element unpacks to: 20 and: 21
>for sub0, sub1 in tpl:
... print "each element of tuple unpacked immediately to:",
sub0,"and:", sub1
...
each element of tuple unpacked immediately to: 0 and: 1
each element of tuple unpacked immediately to: 10 and: 11
each element of tuple unpacked immediately to: 20 and: 21
>>


- Paddy.
Oct 21 '06 #13
John Salerno wrote:
I understand that t returns a single tuple that contains other tuples.
t *is* a single tuple that contains other tuples.
Then 'for x in t' returns the nested tuples themselves.

But what I don't understand is why you can use 'for x,y in t' when t
really only returns one thing. I see that this works, but I can't quite
conceptualize how.
hint:
>>t = (('hello', 'goodbye'),
.... ('more', 'less'),
.... ('something', 'nothing'),
.... ('good', 'bad'))
>>t[0]
('hello', 'goodbye')
>>t[1]
('more', 'less')
>>t[2]
('something', 'nothing')
>>t[3]
('good', 'bad')

</F>

Oct 21 '06 #14
John Salerno a écrit :
jo********@gmail.com wrote:
>It's just sequence unpacking. Did you know that this works?:

pair = ("California","San Francisco")
state, city = pair
print city
# 'San Francisco'
print state
# 'California'


Yes, I understand that. What confused me was if it had been written like
this:

pair = (("California","San Francisco"))

Python 2.4.1 (#1, Jul 23 2005, 00:37:37)
[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on
linux2
Type "help", "copyright", "credits" or "license" for more information.
>>(("California","San Francisco")) == ("California","San Francisco")
True
pair = "California","San Francisco"
>>pair
('California', 'San Francisco')
>>t = 1,
t
(1,)
>>t = 1, 2
t
(1, 2)
>>>
HTH

Oct 21 '06 #15
Paddy wrote:
What I am interested in is if John and others might just take time out
to critique the replies. I'm interested in what the group think makes a
good comp.lang.python reply: too short, too long; too cryptic, too
simplistic, too polite (is their such a thing), too nasty; too
self-effacing, too self-promoting; too long a sig ;-) , too anonymous
Thanks for the reply. It's kind of hard for me to critique your reply at
this point, because I'm starting to understand my problem now. But what
I liked about your help was that you took it in steps and show each
process. What's always a little confusing is when people immediately
resort to examples for help instead of explaining it in words first,
because often times this just duplicates the confusion for the OP. But
examples definitely help!
Oct 23 '06 #16
>>>>Carsten Haese <ca*****@uniqsys.com(CH) wrote:
>CHOn Fri, 2006-10-20 at 15:14, John Salerno wrote:
>>What seems to be happening is that 'for x,y in t' is acting like:

for x in t:
for y,z in x:
#then it does it correctly
>CHNo, it's actually behaving like
>CHfor x in t:
CH y,z = t
y,z = x
>CH # do something with y and z
--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: pi**@vanoostrum.org
Nov 7 '06 #17

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

Similar topics

32
by: Dave Benjamin | last post by:
Hey all, I just realized you can very easily implement a sequence grouping function using Python 2.3's fancy slicing support: def group(values, size): return map(None, * for i in...
37
by: Jon Perez | last post by:
I saw this code snippet: sock.listen(20) for _ in range(20): newsock, client_addr = sock.accept() print "Client connected:", client_addr data = "" why use _ for this example? Is there any...
8
by: Paul McGuire | last post by:
I'm trying to manage some configuration data in a list of tuples, and I unpack the values with something like this: configList = for data in configList: name,a,b,c = data ... do something...
5
by: Chris | last post by:
Hi I'm attempting to write a client for an existing p2p network. The protocol defines that ints are packed into 4 bytes for transfer. // Creating the byte vector using the following is fine:...
4
by: Claudio Grondi | last post by:
I need to unpack on a Windows 2000 machine some Wikipedia media .tar archives which are compressed with TAR 1.14 (support for long file names and maybe some other features) . It seems, that...
74
by: Suyog_Linux | last post by:
I wish to know how the free()function knows how much memory to be freed as we only give pointer to allocated memory as an argument to free(). Does system use an internal variable to store allocated...
5
by: ram | last post by:
Stupid question #983098403: I can't seem to pass an unpacked sequence and keyword arguments to a function at the same time. What am I doing wrong? def f(*args, **kw): for a in args: print...
5
by: Soren S. Jorgensen | last post by:
Hi, I'm trying to read some messages (native structs) from a kernel mode mini-filter driver. I'm using my own implementation of IAsyncResult to pack/unpack the NativeOverlapped structure, and...
21
by: Martin Geisler | last post by:
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkjlQNwACgkQ6nfwy35F3Tj8ywCgox+XdmeDTAKdN9Q8KZAvfNe4 0/4AmwZGClr8zmonPAFnFsAOtHn4JhfY =hTwE -----END PGP...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
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...
0
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...
0
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...
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,...

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.