Process tuple contents on the fly

G

Gnarlodious

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
 
T

Tim Chase

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.
 
B

Barrett Lewis

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.
 
P

Peter Otten

Tim said:
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]
 
T

Tim Chase

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
 
M

MRAB

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))}
 
T

Tobiah

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
 
M

Michael Torrie

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)
 
G

Gnarlodious

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top