Python List Program To Copy List

By | March 23, 2020

Topic: Python List Program To Copy List

# Python program to copy n items 
# List using copy() method
# 
  
def main():
    
    # create empty list so that user will input at runtime
    list1= [] 
  
    # ask the user to input n / how many items?
    
    num = int(input("How many items you wish to Enter in List:")) 
  
    # Use for loop to enter n items in the list 
    for i in range(0, num): 
        item = input("Enter items "+str(i+1)+" in List: ")
        list1.append(item)
    
    #copy list
    new_list = list1.copy()

    # print origional list
    print("The origional List is:",list1)

    # print copied list 
    print("The copied List (using copy() method) is\n", new_list)

main()    

Output

How many items you wish to Enter in List:5
Enter items 1 in List: 123
Enter items 2 in List: hello
Enter items 3 in List: 56777
Enter items 4 in List: zeb
Enter items 5 in List: 67
The origional List is: ['123', 'hello', '56777', 'zeb', '67']
The copied List (using copy() method) is
 ['123', 'hello', '56777', 'zeb', '67']
  

Leave a Reply

Your email address will not be published. Required fields are marked *