0

I want to print an array using an input. This is an example of what I'm trying to do with a lot less arrays:

# Input
x = input()

# Array
b = [1, 2, 3, 4, 5]
c = [2, 3, 4]
d = [1, 2, 3]
#Print Array
print(x)

If someone is to input b it would print:

'b'

Instead of:

[1, 2, 3, 4, 5]

Is there anyway I can fix this without using an if statement for every array?

DarthGir
  • 3
  • 1

1 Answers1

1

Use a dictionary data type:

# Input
x = input()

# Array
arraysByLetter = {}
arraysByLetter['b'] = [1, 2, 3, 4, 5]
arraysByLetter['c'] = [2, 3, 4]
arraysByLetter['d'] = [1, 2, 3]

# Print Array
print(arraysByLetter[x])
Sean
  • 1,279
  • 9
  • 17