iterable-unpacking

Python: Unpacking an inner nested tuple/list while still getting its index number

Python: Unpacking an inner nested tuple/list while still getting its index number Question: I am familiar with using enumerate(): >>> seq_flat = (‘A’, ‘B’, ‘C’) >>> for num, entry in enumerate(seq_flat): print num, entry 0 A 1 B 2 C I want to be able to do the same for a nested list: >>> seq_nested …

Total answers: 1

What does the star and doublestar operator mean in a function call?

What do ** (double star/asterisk) and * (star/asterisk) mean in a function call? Question: In code like zip(*x) or f(**k), what do the * and ** respectively mean? How does Python implement that behaviour, and what are the performance implications? See also: Expanding tuples into arguments. Please use that one to close questions where OP …

Total answers: 4

Meaning of using commas and underscores with Python assignment operator?

Meaning of using commas and underscores with Python assignment operator? Question: Reading through Peter Norvig’s Solving Every Sudoku Puzzle essay, I’ve encountered a few Python idioms that I’ve never seen before. I’m aware that a function can return a tuple/list of values, in which case you can assign multiple variables to the results, such as …

Total answers: 4

Python update object from dictionary

Python update object from dictionary Question: Is there a built-in function/operator I could use to unpack values from a dictionary and assign it into instance variables? This is what I intend to do: c = MyClass() c.foo = 123 c.bar = 123 # c.foo == 123 and c.bar == 123 d = {‘bar’: 456} c.update(d) …

Total answers: 4

Django – How to do tuple unpacking in a template 'for' loop

Django – How to do tuple unpacking in a template 'for' loop Question: In my views.py, I’m building a list of two-tuples, where the second item in the tuple is another list, like this: [ Product_Type_1, [ product_1, product_2 ], Product_Type_2, [ product_3, product_4 ]] In plain old Python, I could iteration the list like …

Total answers: 5