- 2

Change variable name at every iteration

if col1="" ,col2="", col3="", col4="" J=50 for i in range (1,5): col1=J At next iteration of i=2 , col2=J At next iteration of i=3 , col2=J At next iteration of i=4 , col2=J How to do that

9th May 2022, 2:34 AM
Neha Sharma
6 odpowiedzi
+ 2
Didn't you understand what was suggested in your earlier post? if you didn't understand then you should confirm the contributor in your earlier post rather than reposting with minor adjustment. https://www.sololearn.com/Discuss/3029832/?ref=app
9th May 2022, 3:18 AM
Ipang
+ 2
Neha Sharma , - first of all, please mention the programming language you want to use - i don't think that there is a real need to change the variable names, also this not a good programming style, diffucult to debug and to maintain - if you can post a link to your code here, we can surely find a pythonic way of solving your task - please also privide input and expected output samples
9th May 2022, 1:53 PM
Lothar
Lothar - avatar
+ 2
zexu knub actually in python you can. It's just a *very* bad idea in about 99.9999999999999999999% of cases where people want to do it. It's also usually an indicator that people don't understand dictionaries.
9th May 2022, 2:10 PM
Simon Sauter
Simon Sauter - avatar
+ 2
I don't think there is any way to create variables dynamically in python but you can use dictionaries to achieve same sort of behaviour. col_dict = {} for i in range(4): var = "col" + str(i+1) col_dict[var] = [] Here, i have create an empty dict and inside, there are key and values with keys being column names (created with the logic you needed) and values are just empty lists. You can append any item to any list using: col_name = "col2" col_dict[col_name].append("Hello") https://code.sololearn.com/cj5G45B5XKV2/?ref=app
9th May 2022, 4:11 PM
Sandeep
Sandeep - avatar
+ 1
Col=["" for _ in range(4)]
9th May 2022, 5:50 AM
Oma Falk
Oma Falk - avatar
0
You can't, You can however make a list of col & then you may assign dynamic values to each.
9th May 2022, 2:58 AM
zexu knub
zexu knub - avatar