-3

its been a while since I worked in python. I have two lists (A and B). If the user types in 'A', I want the output to be list A.

I can't seem to remember how to do it.

person = input('List A or B?: ')
person = str(input())

A = ["Mark","Rob","Mary"]

B = [ "Alex","Mitch","Tyler"]



for x in A:
    print x
ameyCU
  • 16,489
  • 2
  • 26
  • 41
omonoia
  • 87
  • 4
  • You mean like this `if person == 'A': myList=A else: myList=B` .... `for x in myList`? – Harvey Jun 19 '16 at 03:59
  • @Harvey you could shorten that into a ternary statement or add an elif to check if input is not either – Andrew Li Jun 19 '16 at 04:00
  • @AndrewL: I was going to do that, but thought that they might be simplifying the problem for the question and that a ternary statement then wouldn't be appropriate. You're right, though. I should have added the `elif`, too. Just difficult to know what they really want. – Harvey Jun 19 '16 at 04:02
  • Ahh, I see. I agree fully @Harvey – Andrew Li Jun 19 '16 at 04:02
  • 1
    this is really simple and seems like homework. – Akash Lodha Jun 19 '16 at 04:03
  • 1
    @AkashLodha - I agree. I think "can't seem to remember how" is being used as a euphemism for "don't want"... – TigerhawkT3 Jun 19 '16 at 06:23

5 Answers5

1
person_dict = {
    'A': ["Mark","Rob","Mary"],
    'B': [ "Alex","Mitch","Tyler"]
}
key = raw_input("List A or B:")
print(persons_dict.get(key, None))
Rahul Vivek
  • 196
  • 1
  • 3
  • It doesn't end up making a difference in this particular case, but the parentheses are not needed for `print` in Python 2. It's only one object, though, so, again, it won't change the output. Similarly, `dict.get()` already returns `None` by default for missing keys. No need to specify it. – TigerhawkT3 Jun 19 '16 at 08:09
0

Just make sure to check what the response is, and you're set:

person = input("List A or B? ")

listA = ["Mark","Rob","Mary"]

listB = ["Alex","Mitch","Tyler"]

if person == "A":
    for x in listA:
        print(x)

elif person == "B":
    for x in listB:
        print(x)

Note - When giving the interactive response, you must quote it. input() interprets the input, and you will get an error if you reply B instead of "B".

MadisonCooper
  • 236
  • 2
  • 15
  • Awesome. Thanks, this helped! :) – omonoia Jun 19 '16 at 04:38
  • About `input`: No sane programmer uses this in Python 2.x (see [this question](http://stackoverflow.com/questions/7709022/is-it-ever-useful-to-use-pythons-input-over-raw-input)) – Matthias Jun 19 '16 at 08:02
  • @Matthias - Actually, this answer seems to be geared toward Python 3. In Python 2, it wouldn't even work (`person` would be a list rather than a string). – TigerhawkT3 Jun 19 '16 at 08:07
  • @TigerhawkT3: Yes, this seems to be Python 3 while the OP uses Python 2. – Matthias Jun 19 '16 at 08:09
  • Hey, Im just trying to give the most similar answer to the question. They used input, so I made input work. @Matthias – MadisonCooper Jun 19 '16 at 08:20
0

One possible answer in python 3

def main():
    person = input('List A or B?: ')

    A = ["Mark","Rob","Mary"]

    B = [ "Alex","Mitch","Tyler"]

    myList = []
    if person == 'A':
        myList = A
    elif person == 'B':
        myList = B

    for x in myList:
        print(x)

if __name__ == "__main__":main()

I made 'myList' initially empty so it won't display anything if the user doesn't choose one of the options.

brw59
  • 502
  • 4
  • 19
0

You can do like this:

a = ["Mark","Rob","Mary"]

b = [ "Alex","Mitch","Tyler"]

person = raw_input('List A or B?: ').lower()

if person == 'a':
    print a

elif person == 'b':
    print b

else:
    print 'Try again'
CasperTN
  • 441
  • 1
  • 5
  • 14
-2

There are several ways to do this, some of which are explained by other users.

I'll show you the quickest (yet most unsafe) way to do this:

# Python 3
person = input()
for x in globals()[person]:
    print(x)

Example:

>>> A = ['foo', 'bar']
>>> person = input()
A
>>> person
'A'
>>> globals()[person]
['foo', 'bar']
>>> for x in globals()[person]:
...     print(x)
foo
bar
oxalorg
  • 2,768
  • 1
  • 16
  • 27
  • The person who downvoted this, care to give an explanation? I think I made it clear enough that it's a quick hack. – oxalorg Jun 19 '16 at 05:07
  • 4
    This code makes no sense. You're using `input` in Python 2, which will already evaluate the entered string as a reference to the desired list, and then you're turning that list into a string, and then you're trying to access `globals()` with a key of that stringified list. It is 100% broken. – TigerhawkT3 Jun 19 '16 at 06:20
  • @TigerhawkT3 I'm sorry, I was in a hurry and just copy-pasted the code from the OP and made the required changes without reviewing them. I have no experience with python2 whatsoever, so I've fixed the code to work for python3 with an appropriate example. – oxalorg Jun 19 '16 at 06:52