How can I do assignment in a List Comprehension? [duplicate]

2024/11/15 11:14:24

Generally, whenever I do a for loop in python, I try to convert it into a list comprehension. Here, I have a for loop where a variable value is altered after each loop.

k=5
for x in range(1,6):k*=xprint(k)
#output
5
10
30
120
600

I want to perform this operation in list comprehension. I tried doing but I was getting syntax error. I tried this below:

[k*=x for x in range(1,6)]
Answer

You can use walrus operator (python 3.8+):

k = 5
output = [k := k * x for x in range(1, 6)]print(output) # [5, 10, 30, 120, 600]

But this pattern is not welcomed by some people.

Another option would be to use itertools.accumulate:

from itertools import accumulate
from operator import muloutput = accumulate(range(1, 6), mul, initial=5)print(*output) # 5 5 10 30 120 600

In this case the initial value 5 is attached at the beginning.

https://en.xdnf.cn/q/120461.html

Related Q&A

KeyError: column_name

I am writing a python code, it should read the values of columns but I am getting the KeyError: column_name error. Can anyone please tell me how to fix this issue. import numpy as np from sklearn.clust…

Find starting and ending indices of list chunks satisfying given condition

I am trying to find the start and stop indices of chunks of positive numbers in a list.cross = [7,5,8,0,0,0,0,2,5,8,0,0,0,0,8,7,9,3,0,0,0,3,2,1,4,5,0,0,0,7,5] For the given example input, the desired o…

How can I Scrape Business Email Contact with python?

this morning I wanted to create a little Software/Script in Python, it was 6am when I started and now Im about to become crazy because its 22pm and I have nothing that works.So basically, I want to do …

Python class that works as list of lists

Im trying to create a python class that can work as a list of lists. However, all Ive managed to develop so far is,class MyNestedList(list): ...Im aware that the above code will work as,my = MyNestedLi…

GPA Python Assignment [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Tuple Errors Python

I opened Python and attempted to run the following script (btw, this script is what was directly given to me, I havent edited it in any way as its part of an assignment aside from entering the username…

Flatten list of lists within dictionary values before processing in Pandas

Issue:If I need to flatten a list of lists I use something like this list comprehension to flatten into a single list:[item for sublist in l for item in sublist]I have a dictionary where some of the va…

how to analyse and predict(machine learning) a time series data set using scikit-learn for python

i got data-set like this i need to analyse and predict the status column. This is just 2 entrees from the training data set. In this data set there is heart rate pattern(which is collected in 1 second …

Datetime - Strftime and Strptime

Date = datetime.datetime.today().strftime("%d %B %Y") x = datetime.datetime.strptime(Date , "%d %B %Y")returns:2018-05-09 00:00:00instead of: 9 May 2018, what am I doing wrong? Ess…

Subset sum overlapping dilemma recursively

Im studying recursive logic that one of the problems is subset sum. AFAI read, there are overlaps when it is run by recursion. But, I cannot figure out how there are. Also, I read that to overcome the …