Skip to main content

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
7 votes
2 answers
116 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. ...
Della's user avatar
  • 1,550
0 votes
1 answer
42 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 =...
Jme's user avatar
  • 3
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 ...
Xoriun's user avatar
  • 117
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 ...
TechBarbie's user avatar
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 ...
king Carrey's user avatar
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[...
Saketh's user avatar
  • 21
0 votes
1 answer
72 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....
caseycronyn's user avatar
-2 votes
1 answer
110 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 [...
WaqarQureshii's user avatar
-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 = ...
EstherPM's user avatar
0 votes
1 answer
124 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 ...
r0h4ap's user avatar
  • 25
0 votes
0 answers
95 views

Python Match Case: checking for types inside of iterable of unknown length

I want to check an iterable of unknown length (let's say a list ) contains only a given type (let's say float) using match case (there are other cases, only this one gives me problems). case [*elems] ...
Ernest Klimann's user avatar
4 votes
1 answer
249 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 = ...
Mateo Vial's user avatar
0 votes
2 answers
422 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/...
megha's user avatar
  • 23
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 @...
Karan Kumar's user avatar
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, ...
Copacetic11's user avatar

15 30 50 per page
1
2 3 4 5
33