Created
November 16, 2020 13:33
-
-
Save sshehrozali/72c15f762cb7e1bf196423f6e23b6d68 to your computer and use it in GitHub Desktop.
Python program to demonstrate the use of list.insert()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python program to demonstrate the use of list.insert() # Developed By Syed Shehroz Ali | |
# New List | |
my_list = ["Hello", "World", "Cat", "Dog"] | |
# Print List | |
print("\nList:", my_list) | |
# Print Total length of list | |
print("\nTotal Length of list: ", len(my_list), "\n") | |
print("Each word in list with index no") | |
# Print each word in List with index no | |
for i in range(len(my_list)): | |
print(i, ":", my_list[i]) | |
# Insert new word in List at index no = 3 | |
my_list.insert(3, "Mouse") | |
print("\nUpdated word in list with index no") | |
# Print each word in updated List with index no | |
for i in range(len(my_list)): | |
print(i, ":", my_list[i]) | |
# Indicate the inserted word at index no = 3 | |
if i == 3: | |
print(i, ":", my_list[i], " <- This is the inserted word") | |
# Print new line, end the program | |
print("\n") | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment