Pyhton List manipulation example

 #1. Enter a string

myFavAnimals ='lion cheetah zebra tiger kob rhino buffalo'


#2.Split the string into word list

a = myFavAnimals.split()

print("My string words separated are: ",a)


#3.Delete three words from list using three different python operations

#a. Delete using remove() function

b = a.remove('lion')

print("My string words after deleting first word: ",a)

#b. Delete using pop() function with position specified

b1 = a.pop(3)

print("My string words after deleting second word: ",a)

#b. Delete using pop() function with position not  specified

b2 = a.pop()

print("My string words after deleting third word: ",a)


#4.Sort the list

c = a.sort()

print("My string words after sorting: ",a)


#5. Add new words to list using three different python operations

#a. Add new word using append() function

d= a.append('Gorilla')

print("My string words after adding first word: ",a)

#b. Add new word using insert() function

d= a.insert(4,'Monkey')

print("My string words after adding second word: ",a)

#b. Add new word using extend() function

d= a.extend(['giraffe','antelope','bear'])

print("My string words after adding more three words: ",a)


#6. Join the split words into string

e = '  '.join(a)

print("My string words after joining: ",e)


Comments

Popular posts from this blog

English to Runyankole speech translation app

Privacy policy

English to Runyankole translation app