488 questions
0 votes
1 answer
59 views
How to achieve Python-like tuple unpacking in DolphinDB SQL to dynamically select columns?
In Python, I can unpack a list into individual variables: >>> name,age,date = ['Bob',20,'2025-1-1'] >>> name 'Bob' >>> age 20 >>> date '2025-1-1' In DolphinDB, I ...
1 vote
4 answers
162 views
list comprehension with iterable unpacking?
My input is in the form of pairs of comma-separated values, e.g., 805i,9430 3261i,9418 3950i,9415 4581i,4584i 4729i,9421 6785i,9433 8632i,9434 9391i,9393i and I want to read them into a list of pair ...
2 votes
1 answer
108 views
Python-like Iterable Unpacking in C++
In python, we can unpack an iterable with a unary * like so: def foo(a, b, c): ... t = (1, 2, 3) foo(*t) In C++, I haven't encountered an equivalent. I'm aware that I could use structured ...
0 votes
1 answer
199 views
List unpacking in python
what seems to be the problem with the code l = [1,2,4,5,6,7,8] t = *l i have read that *l gives comma separated single values so the statement should be similar to t = 1,2,3,4,5,6,7 which is a tuple ...
1 vote
3 answers
228 views
Why does Python tuple unpacking work on sets?
Sets don't have a deterministic order in Python. Why then can you do tuple unpacking on a set in Python? To demonstrate the problem, take the following in CPython 3.10.12: a, b = {"foo", &...
1 vote
1 answer
60 views
Unpacking the last arguments with star spread operator in Groovy closure
Groovy can unpack lists or tuples with the spread operator (*), which looks like in Python. In Python we can partially unpack a tuple, like this: mytuple = ('a', 'b', 'c', 'd') first, *others = ...
0 votes
3 answers
223 views
Python f-string equivalent of iterable unpacking by print instruction
print can nicely unpack a tuple removing brackets and commas, e.g. a = (0, -1, 1) print(*a) produces 0 -1 1. Trying the same with f-strings fails: print(f'{*a}') The closest f-string option I found ...
0 votes
3 answers
89 views
Unpacking Tuples into two tuples
Can a tuple be unpacked to two tuples? E.g.: countries=("China","Japan","India","England","France") – unpack this tuple to two tuples, first tuple ...
-1 votes
2 answers
44 views
I am trying to use tuple unpacking for a custom iterable. The iterable size should be increase as the number of variables in the lhs
Write a python class that gives the expected output Currently gives ValueError # Can be modified. (Can even be a function or generator class MyClass: def __init__(self): self.count = 0 ...
2 votes
4 answers
165 views
Why can I unpack a Python set when sets are unordered?
I am quite used to unpacking sequences in Python like: my_tuple = (1, 2, 3) a, b, c = my_tuple I have noticed that I can also do it with sets: my_set = set((1, 2, 3)) a, b, c = my_set Why can I do ...
0 votes
0 answers
66 views
Why is the SWAP instruction missing in Python 3.11's disassembled bytecode for tuple swaps?
In newer versions, ROT_TWO is replaced by SWAP. However, after disassembling the function using dis.dis(), I noticed that SWAP does not appear in the bytecode. Python 3.9.0 >>> import dis >...
-1 votes
3 answers
101 views
What is the correct way to unpack a tuple composed of a TreeNode and an Integer?
I'm implementing an algorithm that requires appending and popping nodes from a Tree in python (in a FIFO way). queue = [] # empty list root = TreeNode() # a standard TreeNode with val, left and ...
5 votes
2 answers
174 views
Do Python coders have a bias towards list over tuple? [closed]
Basic Facts Lists are mutable (supporting inserts, appending etc.), Tuples are not Tuples are more memory efficient, and faster to iterate over So it would seem their use-cases are clear. ...
0 votes
1 answer
50 views
Why does comprehension only work with tuples when unpacking in Python?
I tried to use list comprehension on a tuple and it worked fine when unpacking, but not when assigning to a single variable. If I run the following code var1, var2, var3 = (i for i in range(3)) var1 =...
0 votes
4 answers
81 views
Unpacking into several lists of fixed length
I have a list of data (of varying type) that I need to process. It has fixed length and I know which index holds what data, so currently I do something like for x in data[:13]: do_stuff(x) for x in ...