iterable-unpacking Questions
2
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. F...
Tana asked 8/7 at 2:16
5
Solved
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 argumen...
Friedrick asked 27/5, 2010 at 14:10
5
Solved
I have the following:
>>> myString = "has spaces"
>>> first, second = myString.split()
>>> myString = "doesNotHaveSpaces"
>>> first, second = myString.split()
T...
Incondensable asked 29/7, 2016 at 19:33
2
Solved
In python, I can write something like this:
some_list = [(1, 2, 3), (3, 2, 1)]
for i, *args in some_list:
print(args)
I will get the next output:
[2, 3]
[2, 1]
When we use *args as function arg...
Malanie asked 29/10, 2021 at 7:34
6
Solved
I used an answer to the SO question "iterate over tuple" to write a method to overload <<. This method was tested and appears to work correctly with g++ 4.7 on Debian squeeze.
However this m...
Hypothermal asked 12/2, 2012 at 9:25
2
Solved
I have a list like this:
[('love', 'yes', 'no'), ('valentine', 'no', 'yes'), ('day', 'yes','yes')]
How do I split this list into three variables where each variable holds one tuple, i.e.
var1 = ('...
Droppings asked 14/2, 2014 at 23:48
10
Solved
I'm trying to store in a std::tuple a varying number of values, which will later be used as arguments for a call to a function pointer which matches the stored types.
I've created a simplified exa...
Cartercarteret asked 22/10, 2011 at 10:19
2
Solved
Is it possible to use type hinting when unpacking a tuple? I want to do this, but it results in a SyntaxError:
from typing import Tuple
t: Tuple[int, int] = (1, 2)
a: int, b: int = t
# ^ SyntaxEr...
Embezzle asked 29/8, 2018 at 17:16
11
Solved
I have only a single key-value pair in a dictionary. I want to assign key to one variable
and it's value to another variable. I have tried with below ways but I am getting error for same.
>>...
Spaceman asked 22/11, 2013 at 13:18
5
Solved
Can anyone explain the difference when unpacking the dictionary using a single or a double asterisk? You can mention their difference when used in function parameters, only if it is relevant here, ...
Jandy asked 5/11, 2018 at 3:45
8
Solved
I'm trying to write a function that turns strings of the form 'A=5, b=7' into a dict {'A': 5, 'b': 7}. The following code snippets are what happen inside the main for loop - they turn a single part...
Shedevil asked 23/8, 2012 at 15:8
3
Solved
I've often been frustrated by the lack of flexibility in Python's iterable unpacking.
Take the following example:
a, b = range(2)
Works fine. a contains 0 and b contains 1, just as expected. No...
Carlynne asked 18/6, 2017 at 18:46
3
Solved
When a Python list is known to always contain a single item, is there a way to access it other than:
mylist[0]
You may ask, 'Why would you want to?'. Curiosity alone. There seems to be an alternat...
Radiothermy asked 16/10, 2015 at 2:9
8
Solved
I stumbled across the following code:
for i, a in enumerate(attributes):
labels.append(Label(root, text = a, justify = LEFT).grid(sticky = W))
e = Entry(root)
e.grid(column=1, row=i)
entries.ap...
Taunyataupe asked 3/6, 2012 at 4:24
1
Solved
Imagine I have an object which is an instance of a class such as the following:
@dataclass
class Foo:
bar: int
baz: str
I'm using dataclasses for convenience, but in the context of this question...
Surgical asked 9/11, 2021 at 22:56
4
Solved
If I have a tuple such as (1,2,3,4) and I want to assign 1 and 3 to variables a and b I could obviously say
myTuple = (1,2,3,4)
a = myTuple[0]
b = myTuple[2]
Or something like
(a,_,b,_) = myTuple
...
Removal asked 2/3, 2012 at 11:32
4
I'm thinking if there is some way to unpack object attributes.
Usually doing this involves series of:
self.x = x
self.y = y
... #etc.
However it should be possible to do it better.
I'm thinking...
Millennium asked 24/9, 2013 at 4:16
1
This has to do with the new Python 3.10 beta and the new match syntax.
Is there any way to check if a pattern is simply contained in an iterable? the most obvious solution, to simply put two wildca...
Opaline asked 21/5, 2021 at 12:17
8
Solved
In mathematics and computer science, a tuple is an ordered list of elements. In set theory, an (ordered) n-tuple is a sequence (or ordered list) of n elements, where n is a positive integer.
So...
Absorptivity asked 30/7, 2011 at 16:0
4
Solved
Consider the following expressions. Note that some expressions are repeated to present the "context".
(this is a long list)
a, b = 1, 2 # simple sequence assignment
a, b = ['green', 'blue...
Mead asked 6/8, 2011 at 14:59
2
Solved
I am trying to create a list based on another list, with the same values repeated 3 times consecutively.
At the moment, I am using:
>>> my_list = [ 1, 2 ]
>>> three_times = []
>...
Antiscorbutic asked 17/5, 2016 at 14:56
7
Solved
I have a dataframe with a timeindex and 3 columns containing the coordinates of a 3D vector:
x y z
ts
2014-05-15 10:38 0.120117 0.987305 0.116211
2014-05-15 10:39 0.117188 0.984375 0.122070
2014-...
Fugacity asked 15/5, 2014 at 23:21
1
Solved
I am writing a class that defines __iter__ and __len__, where the value of __len__ depends on the iterator returned by __iter__. I am getting an interesting RecursionError.
Language versions: Pytho...
Inexplicable asked 23/12, 2020 at 17:15
3
Solved
I know the canonical way to unpack a tuple is like this
a, b, c = (1, 2, 3)
# or
(a,b,c) = (1, 2, 3)
but noticed that you can unpack a tuple like this
[a, b, c] = (1, 2, 3)
Does the second ...
Harborage asked 1/12, 2020 at 1:26
2
Solved
What is the difference in Python between unpacking a function call with [], with () or with nothing?
def f():
return 0, 1
a, b = f() # 1
[a, b] = f() # 2
(a, b) = f() # 3
Mucilaginous asked 29/10, 2020 at 13:3
1 Next >
© 2022 - 2024 — McMap. All rights reserved.