Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable in for loop
#1
Hello guys i have simple code like this. Please can someone give me example how to update this code to make for every name in list new variable ? I just want this for loop to make 3 variables with 3 names from list. Name1 = samuel, name2 = max, name3 = john

Thank you

names = ["samuel","max","john"]

counter = 0

for i in names:
    name1 = names[counter]
    counter +=1


print(name1)
Reply
#2
The short answer is that there is no usage case that justifies doing this. You are much better off referencing your existing list, or you could use a dictionary if that suits your purposes better. Creating dynamically named variables adds unnecessary complexity to your code and makes maintaining it much more difficult.

With that said, it is technically possible to do what you are asking with the exec() function. Again, this is NOT recommended:
names = ["samuel","max","john"]
 
for i, n in enumerate(names):
    exec('name' + str(i + 1) + '= n')
 
print(f'name1 = {name1}, name2 = {name2}, name3 = {name3}')
Output:
name1 = samuel, name2 = max, name3 = john
Reply
#3
I actually get you are tring to do here, but its not possible to do so but alternatively you can use dictionary to do somewhat same.Here is the code
names = ["samuel","max","john"]
dic_names={}
for i,j in zip(names,range(len(names))):
    index="name{}".format(j)
    dic_names[index]=i
print(dic_names)
Output:
{'name0': 'samuel', 'name2': 'john', 'name1': 'max'}
Also you can access each of them by using dictionary. Here is a basic guide to understand dictionaries
Reply
#4
creating names dynamically is something you don't want to do.
use proper data structure
keys = [f'name{i}' for i in range(3)]
values = ["samuel","max","john"]
names = dict(zip(keys, values))
print(names)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
But if you're really just going to call your keys namei where i = 0, 1, 2, ..., n, why bother with a dictionary? It sounds like you just want a sequence (i.e. a list or tuple).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 418 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,666 Nov-07-2023, 09:49 AM
Last Post: buran
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,565 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  loop (create variable where name is dependent on another variable) brianhclo 1 1,130 Aug-05-2022, 07:46 AM
Last Post: bowlofred
  Multiple Loop Statements in a Variable Dexty 1 1,198 May-23-2022, 08:53 AM
Last Post: bowlofred
Big Grin Variable flag vs code outside of for loop?(Disregard) cubangt 2 1,164 Mar-16-2022, 08:54 PM
Last Post: cubangt
  How to save specific variable in for loop in to the database? ilknurg 1 1,142 Mar-09-2022, 10:32 PM
Last Post: cubangt
  How to add for loop values in variable paulo79 1 1,438 Mar-09-2022, 07:20 PM
Last Post: deanhystad
  Using Excel Cell As A Variable In A Loop knight2000 7 4,081 Aug-25-2021, 12:43 PM
Last Post: snippsat
  Using Excel Cell As A Variable In A Loop knight2000 7 4,977 Jul-18-2021, 10:52 AM
Last Post: knight2000

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020