-2

I have some lists with common names and I'd like to use a for loop to concatenate them.

Bitcoin_2014 = [457.00, 639.80, 386.94, 320.19]
Bitcoin_2015 = [244.22, 263.07, 236.06, 430.57]
Bitcoin_2016 = [416.73, 673.34, 609.74, 963.74]

I'd like the for loop to be able to print all lists (since the only thing that changes are the last two digits) with the logic of the following but I know the syntax isn't correct.

for i in range(14, 17):
   print(Bitcoin_20,i)

Are you able to do this in Python?

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • 3
    You really shouldn't do that though. If you have only these three variables, just use their names. If you have lots more variables, use a dictionary. See [How do I create a variable number of variables](//stackoverflow.com/q/1373164/843953) – Pranav Hosangadi Feb 03 '23 at 21:10

1 Answers1

1

You can put all the lists in a tuple and loop over that.

for b in (Bitcoin_2014, Bitcoin_2015, Bitcoin_2016):
    print(b)

Consider using a dict instead if you have many such lists.

bitcoin = {
    2014 : [457.00, 639.80, 386.94, 320.19],
    2015 : [244.22, 263.07, 236.06, 430.57],
    2016 : [416.73, 673.34, 609.74, 963.74]
}
for year, val in bitcoin.items():
    print(year, val)
Unmitigated
  • 76,500
  • 11
  • 62
  • 80