Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43632 > unrolled thread
| Started by | Gnarlodious <gnarlodious@gmail.com> |
|---|---|
| First post | 2013-04-15 11:25 -0700 |
| Last post | 2013-04-15 16:46 -0700 |
| Articles | 10 — 7 participants |
Back to article view | Back to comp.lang.python
Process tuple contents on the fly Gnarlodious <gnarlodious@gmail.com> - 2013-04-15 11:25 -0700
Re: Process tuple contents on the fly Tim Chase <python.list@tim.thechases.com> - 2013-04-15 13:50 -0500
Re: Process tuple contents on the fly Barrett Lewis <musikal.fusion@gmail.com> - 2013-04-15 12:05 -0700
Re: Process tuple contents on the fly Peter Otten <__peter__@web.de> - 2013-04-15 21:07 +0200
Re: Process tuple contents on the fly Tim Chase <python.list@tim.thechases.com> - 2013-04-15 14:10 -0500
Re: Process tuple contents on the fly Barrett Lewis <musikal.fusion@gmail.com> - 2013-04-15 12:16 -0700
Re: Process tuple contents on the fly MRAB <python@mrabarnett.plus.com> - 2013-04-15 20:29 +0100
Re: Process tuple contents on the fly Tobiah <toby@tobiah.org> - 2013-04-15 13:35 -0700
Re: Process tuple contents on the fly Michael Torrie <torriem@gmail.com> - 2013-04-15 15:16 -0600
Re: Process tuple contents on the fly Gnarlodious <gnarlodious@gmail.com> - 2013-04-15 16:46 -0700
| From | Gnarlodious <gnarlodious@gmail.com> |
|---|---|
| Date | 2013-04-15 11:25 -0700 |
| Subject | Process tuple contents on the fly |
| Message-ID | <f51058c3-0e36-429c-85d9-365be82b548f@googlegroups.com> |
Say I have a tuple I want to expand assigning to variables: tup = *func() var = tup[0] lst.append(tup[1]) Or could I do it in one line? var, lst.append() = *func() So I want to append one variable to a list on the fly, is it possible? -- Gnarlie http://gnarlodious.com
[toc] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2013-04-15 13:50 -0500 |
| Message-ID | <mailman.640.1366051720.3114.python-list@python.org> |
| In reply to | #43632 |
On 2013-04-15 11:25, Gnarlodious wrote:
> Say I have a tuple I want to expand assigning to variables:
>
> tup = *func()
> var = tup[0]
> lst.append(tup[1])
>
> Or could I do it in one line?
>
> var, lst.append() = *func()
>
> So I want to append one variable to a list on the fly, is it
> possible?
I stumbled across this atrocity[*], which if you chose to use it,
you'd deserve a kick in the pants:
lst.append("Value I don't care about and will overwrite")
var, lst[-1] = *func()
It's not quite one step, but at least the *assignment* is one step :-)
-tkc
[*] my original discovery was
d = {}
for key, d[key] in (("this",18), ("that",17), ("other",38)):
print key
do_something(d)
but the same applies to a plain ol' assignment statement as to an
assignment in a "for" loop.
[toc] | [prev] | [next] | [standalone]
| From | Barrett Lewis <musikal.fusion@gmail.com> |
|---|---|
| Date | 2013-04-15 12:05 -0700 |
| Message-ID | <mailman.641.1366052737.3114.python-list@python.org> |
| In reply to | #43632 |
[Multipart message — attachments visible in raw view] — view raw
> d = {}
> for key, d[key] in (("this",18), ("that",17), ("other",38)):
> print key
> do_something(d)
>
Why not use a dict comprehension?
d = {k:v for k,v in (("this",18), ("that",17), ("other",38))}
I feel this is more straightforward and easier to read. the results are the
same however.
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-04-15 21:07 +0200 |
| Message-ID | <mailman.642.1366052854.3114.python-list@python.org> |
| In reply to | #43632 |
Tim Chase wrote:
> On 2013-04-15 11:25, Gnarlodious wrote:
>> Say I have a tuple I want to expand assigning to variables:
>>
>> tup = *func()
>> var = tup[0]
>> lst.append(tup[1])
>>
>> Or could I do it in one line?
>>
>> var, lst.append() = *func()
>>
>> So I want to append one variable to a list on the fly, is it
>> possible?
>
> I stumbled across this atrocity[*], which if you chose to use it,
> you'd deserve a kick in the pants:
>
> lst.append("Value I don't care about and will overwrite")
> var, lst[-1] = *func()
>
> It's not quite one step, but at least the *assignment* is one step :-)
I think the star is on the wrong side.
So:
>>> items = ["a", "b", "c"]
>>> def f(result=(4, 5, 6)): return result
...
>>> var, *items[len(items):] = f()
>>> var
4
>>> items
['a', 'b', 'c', 5, 6]
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2013-04-15 14:10 -0500 |
| Message-ID | <mailman.643.1366052947.3114.python-list@python.org> |
| In reply to | #43632 |
On 2013-04-15 12:05, Barrett Lewis wrote:
> > d = {}
> > for key, d[key] in (("this",18), ("that",17), ("other",38)):
> > print key
> > do_something(d)
>
> Why not use a dict comprehension?
> d = {k:v for k,v in (("this",18), ("that",17), ("other",38))}
>
> I feel this is more straightforward and easier to read. the results
> are the same however.
In the particular case I did it in, I needed the incremental results
passed to a function, not just the final result. I don't think this
made it into the final code, rather it was expanded to be more
readable. But the discovery made me feel a disturbance in the
Pythonic force of the universe. :*)
-tkc
[toc] | [prev] | [next] | [standalone]
| From | Barrett Lewis <musikal.fusion@gmail.com> |
|---|---|
| Date | 2013-04-15 12:16 -0700 |
| Message-ID | <mailman.644.1366053447.3114.python-list@python.org> |
| In reply to | #43632 |
[Multipart message — attachments visible in raw view] — view raw
> In the particular case I did it in, I needed the incremental results > passed to a function, not just the final result. I don't think this > made it into the final code, rather it was expanded to be more > readable. But the discovery made me feel a disturbance in the > Pythonic force of the universe. :*) > > -tkc > I see what you are saying, but I agree that is a disturbance I didn't even know you could do for key, d[key], that just feels like bad news. however for what you are saying it makes sense. TIL that you can use an unpacked value during unpacking!
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2013-04-15 20:29 +0100 |
| Message-ID | <mailman.645.1366054372.3114.python-list@python.org> |
| In reply to | #43632 |
On 15/04/2013 20:05, Barrett Lewis wrote:
>
>
>
> d = {}
> for key, d[key] in (("this",18), ("that",17), ("other",38)):
> print key
> do_something(d)
>
>
> Why not use a dict comprehension?
> d = {k:v for k,v in (("this",18), ("that",17), ("other",38))}
>
> I feel this is more straightforward and easier to read. the results are
> the same however.
>
Why use a dict comprehension? :-)
d = dict((("this",18), ("that",17), ("other",38))}
[toc] | [prev] | [next] | [standalone]
| From | Tobiah <toby@tobiah.org> |
|---|---|
| Date | 2013-04-15 13:35 -0700 |
| Message-ID | <nvZat.526374$Hg7.243527@newsfe30.iad> |
| In reply to | #43632 |
On 04/15/2013 11:25 AM, Gnarlodious wrote: > Say I have a tuple I want to expand assigning to variables: > > tup = *func() What is the asterisk for? I assume it's a python 3 thing, because I get a syntax error, but I'm having trouble Googling it. Thanks, Tobiah
[toc] | [prev] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2013-04-15 15:16 -0600 |
| Message-ID | <mailman.648.1366060603.3114.python-list@python.org> |
| In reply to | #43642 |
On 04/15/2013 02:35 PM, Tobiah wrote: > On 04/15/2013 11:25 AM, Gnarlodious wrote: >> Say I have a tuple I want to expand assigning to variables: >> >> tup = *func() > > What is the asterisk for? I assume it's a python 3 > thing, because I get a syntax error, but I'm having > trouble Googling it. No it's not. It's a tuple unpack operator. It's commonly used in this context: def func1(*args, **kwargs): #func1 can take variable args # do stuff func2( *args ) #unpack the variable args and pass them to func2 func3( *args ) func4( *args, **kwargs) def func2( a, b, c): d = a + b + c def func3 ( *args ): pass def func4 ( *args, **kwargs): pass func1(1,2,3)
[toc] | [prev] | [next] | [standalone]
| From | Gnarlodious <gnarlodious@gmail.com> |
|---|---|
| Date | 2013-04-15 16:46 -0700 |
| Message-ID | <de91c74f-56f6-4b28-b10c-82bfd2778026@googlegroups.com> |
| In reply to | #43642 |
On Monday, April 15, 2013 2:35:10 PM UTC-6, Tobiah wrote: > > tup = *func() > What is the asterisk for? I assume it's a python 3 Not Python 3, pseudocode. I should have said as such, sorry. Supposed to indicate an expanded tuple. -- Gnarlie
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web