I’m confused on how I print a specific users input based on another input entered by a user. For example if I had a sample input of:
A A B B
1 3
I would want it to output A and B since A is the first element and B is the third.
How would I do this with a code like this:
user = input().split()
user1 = input()
print()#Don't know what to do from here
Any help would be appreciated. Thank you.
>Solution :
Try using iteration to take values from user1 and get the values of user with indexes from user1.
So something like so:
values = input().split(" ") #changed user to values for readability
indexes = input().split(" ") #changed user1 to indexes
print(" ".join([values[int(i)-1] for i in indexes if int(i) <= len(values)]))
This uses list comprehension to simplify the iteration we need to use and join to convert the resultant list into a string.