简体   繁体   中英

Input element from one list to add to another

I am very early in my python journey so my coding knowledge is very basic. Simple answers appreciated.

My challenge here is to create a function to select one item from a list to add to another list

Here is a Pseudo Code of the general idea:

# My Lists
list1 = ["item1", "item2", "item3", "item4", "item5"]
list2 = []

# print verticle, indexed list
for element in (list1):
            print(list1.index(element) + 1, element)

# create function to input selected item an add to list2
def selectitem():
     selection = input("Enter the number of item you'd like to add to list2: ")
     if selection == "1": # How would I now define "1" to select and append index 0 from list1 to list2?
          list2.append(selection)

I hope that was clear. In summary, if I enter "1" into the input, I want to add index 0 to list2. Any concise solutions on how to do this would be greatly appreciated!

#My Lists
list1 = ["item1", "item2", "item3", "item4", "item5"]

list2 = []

#create function to input selected item an add to list2

def selectitem():

    selection = int(input("Enter the number of item you'd like to add to list2: "))

    list2.append(list1[selection-1])

    print(list2)

selectitem()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM