2

I have 4 lists like

list1 = []
list2 = []
list3 = []
list4 = []

then i need get user input and merge two lists at run time and print them ..

I have tried like this

first = input('enter 1st list')
second = input('enter 2nd list ')
if (first == 1):
    first  = list1
elif(first ==2):
    second = list2
....
....

but this is very long procedure and not appropriate for taking many inputs .

So please suggest me a better method..

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
kasinavijay
  • 168
  • 1
  • 12

3 Answers3

2

Put your 4 lists in another list:

lists = [list1, list2, list3, list4]

Now you can address them with indexing:

firstlist = lists[int(first) - 1]
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

try this using dict

lists = {1: list1, 2: list2, 3:list3, 4:list4}

firstlist = lists[int(first)]
Syed Habib M
  • 1,757
  • 1
  • 17
  • 30
0

Just use eval() method in python3.x if you are using python 2.x use raw_input .

user987339
  • 10,519
  • 8
  • 40
  • 45
Vijay Kasina
  • 144
  • 1
  • 8
  • This is *not* the most sound advice. There is no need to use `eval()` when better and more secure options exist. – Martijn Pieters Dec 02 '13 at 12:21
  • Using `eval()` is not recommended especially when there is a better way, you can read more about it [here](http://stackoverflow.com/q/1832940/1982962) – Kobi K Dec 02 '13 at 13:16