kasinavijay
kasinavijay

Reputation: 168

merge lists in runtime according to user inputs

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..

Upvotes: 2

Views: 168

Answers (3)

Vijay Kasina
Vijay Kasina

Reputation: 144

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

Upvotes: 0

Syed Habib M
Syed Habib M

Reputation: 1817

try this using dict

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

firstlist = lists[int(first)]

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1123032

Put your 4 lists in another list:

lists = [list1, list2, list3, list4]

Now you can address them with indexing:

firstlist = lists[int(first) - 1]

Upvotes: 2

Related Questions