-4

Developing on my previous question I'm wondering whether there is a way to have an element in a list and find that position in another list and return what is in the position as a variable. for example:

list1 = [0,1,2,3,4]
list2 = ["0","hello","my","name","is","Daniel"]

if the user enters the number 14 the program will return "hello Daniel" in the sense that the computer reads 14 as 1 and 4 and finds the position in list as an example.

I've tired using [list2.index(x) for x in list1] hoping that it would work but I've been unable to change the code so it does work. is there a simple way to do it

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
D. Forrester
  • 25
  • 1
  • 1
  • 5

2 Answers2

0

I think that this will solve your problem: for x in list1: print list2[x]

Is it that what you were asking ?

Guido
  • 104
  • 1
  • 7
0

You can do something like this :

input_num = 123
list1 = [0, 1, 2, 3, 4] # if list1 is not dervied from input number
list2 = ["0", "hello", "my", "name", "is", "Daniel"]
print(' '.join([list2[list1.index(int(ind))] for ind in str(input_num)]))

This will result in :

hello my name

Also, i would suggest you to look at the https://docs.python.org/2/tutorial/datastructures.html and trying things out to learn.

Satish Prakash Garg
  • 2,213
  • 2
  • 16
  • 25