user13904208
user13904208

Reputation: 21

Python - varible name change

I want to create a dictionary or list of some sort to show the names:

Bubblegum_1
Bubblegum_2
Bubblegum_3
Bubblegum_4
Bubblegum_5
Bubblegum_6 

How to I do a line of code that would dynamically change the name number for me without me touching it?

The desire would be to only have Bubblegum_ + something that can help me do this without me typing it in manually.

Thank you

Upvotes: 0

Views: 63

Answers (2)

Rahul Singh
Rahul Singh

Reputation: 184

You can have your variables in form of Keys of a Dictionary.

data = {}

for i in range(1,10):
  data["Bubblegum_"+str(i)] = i*2

data

Your data variable will look something like this :

{
 'Bubblegum_1': 2,
 'Bubblegum_2': 4,
 'Bubblegum_3': 6,
 'Bubblegum_4': 8,
 'Bubblegum_5': 10,
 'Bubblegum_6': 12,
 'Bubblegum_7': 14,
 'Bubblegum_8': 16,
 'Bubblegum_9': 18
}

And finally you access your variables value like this :

data['Bubblegum_1']

Upvotes: 1

Seaver Olson
Seaver Olson

Reputation: 458

num = 15#or amount of strings you want
bubblegum = list()
for i in range(num):
     bubblegum.append('Bubblegum_' + str(i))
print(bubblegum)
    

Upvotes: 0

Related Questions