Questions tagged [iterable-unpacking]
A Python feature in which elements of an iterable are simultaneously assigned to multiple variables, e.g. a, b, c = [1, 2, 3].
iterable-unpacking
481
questions
-1
votes
3
answers
60
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 ...
7
votes
2
answers
121
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
43
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
60
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 ...
0
votes
0
answers
8
views
How to compare two tuple list, return the highest value and the variable name from which the highest value is found?
So I have tuple in a list in two places
group_a = [(“Yellow”,200),(“Red”,450),(“Pink”,320)]
group_b = [(“Blue”,350),(“Green”,590),(“Purple”,190)]
I’d like to unpack this two groups with def function ...
0
votes
1
answer
85
views
Is there a better way to zip iterables with mask?
I need to zip iterables with a personal mask function or just mask lists [True, False, ...]
from collections.abc import Callable
def zip_mask(a, b, mask):
iter_a, iter_b = iter(a), iter(b)
if ...
1
vote
0
answers
53
views
Tuple unpacking issue in reversing a linked list using tuple unpacking in Python [duplicate]
Reversing Linked List Leetcode-206
Initial Approach:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverseList(self, head: Optional[...
0
votes
1
answer
75
views
Tuple unpacking to formatted string in Python
I have been working though Fluent Python 2nd Edition by Luciano Ramalho.
On page 5 he writes that he has attempted to adopt the f string syntax throughout the book, although there are times when str....
-2
votes
1
answer
114
views
How do I unpack a list with a varying amount of items into individual variables?
import streamlit as st
sidebar_counter = 9
# this number can vary depending on what is selected by the user
column_list = [col1, col2, col3, col4, col5]
# column_list can vary and be a list that is [...
-3
votes
1
answer
101
views
Recursion in a tailored map function - don't understand the program flow
I am learning tail recursion and don't understand the flow of this example.
I am using VSCode debugger and don't understand why when the program reaches return[f(arg), *result] it goes to result = ...
0
votes
1
answer
125
views
Can I put something between two unpacked list in print()?
For example: I have a list filled with ints.
I need to print it from a template(with '\n' between) with only one print():
>*sorted(lst)
>*reversed(lst)
I can't use sep or f-string because of ...
4
votes
1
answer
254
views
How to "pack" a variable amount of outputs from a function in Lua?
I have a function in Lua that gives a variable number of outputs. How can I get all its outputs, regardless of how many outputs it has?
function my_function()
-- whatever
end
outputs_list = ...
0
votes
2
answers
429
views
How can I ungroup a polars dataframe in python?
I have a polars dataframe that has a particular column with repeating patterns. I have grouped them by the patterns & adding a new column to this grouped dataframe. But now I have to unpack/...
0
votes
0
answers
69
views
My Python code runs successfully, but when imported as a module it gives 'cannot unpack non-iterable NoneType object'
import os
import sys
import shutil
from src.logger import logging
from src.exception import CustomException
from dataclasses import dataclass
## intitialize the Data Ingestion configuration
@...
0
votes
1
answer
51
views
Why is my second variable (x2,y2) not defined while my first variable (x1,y1) is?
class Line():
def __init__(self, coor1 , coor2):
self.coor1 = coor1
self.coor2 = coor2
def distance(self):
for x1,y1 in self.coor1, ...