l=[]
n=int(raw_input('Enter the no of elements added to list:'))
for i in range(n):
element=int(raw_input('Enter the elements:'))
l.append(element)
print '\nNew List is ',
m=int(raw_input('\nEnter the no of elements added to list using insert:'))
for i in range(m):
u=int(raw_input('Enter the index to be added'))
u1=int(raw_input('Enter the element to be added'))
l.insert(u,u1)
print '\nNew List after insert operation is: ',l
ch=int(raw_input('\nPress 1 to pop the last element\nPress 2 to pop the element in a particular index\nEnter the choice: '))
if ch==1:
l.pop()
print '\nNew List after deletion is: ',l
elif ch==2:
p=int(raw_input('Enter the element index to be deleted'))
l.pop(p)
print '\nNew List after deletion is: ',l
else:
print '\nInvalid choice'
print ' \nList elements are sorted: ',l
Solve Problems by Coding Solutions - A Complete solution for python programming
To add elements in list using insert
y=[]
n=int(raw_input('Enter the no of elements added to list:'))
index=0
for i in range(n):
element=int(raw_input('Enter the elements:'))
y.insert(index, element)
index=index+1
print y
n=int(raw_input('Enter the no of elements added to list:'))
index=0
for i in range(n):
element=int(raw_input('Enter the elements:'))
y.insert(index, element)
index=index+1
print y
To add elements inside a list using append
l=[]
n=int(raw_input('Enter the no of elements added to list:'))
for i in range(n):
element=int(raw_input('Enter the elements:'))
l.append(element)
print l
n=int(raw_input('Enter the no of elements added to list:'))
for i in range(n):
element=int(raw_input('Enter the elements:'))
l.append(element)
print l
To count the elements in a nested list with all elements are list
a=[[3, 4, 5,8, 8 ], [5, 6, 7], [7, 8, 9]]
def count(a):
j=0
n=0
for b in a:
j=len(b)
n=n+j
return n
t= count(a)
print "No elements in the nested list are:",t
def count(a):
j=0
n=0
for b in a:
j=len(b)
n=n+j
return n
t= count(a)
print "No elements in the nested list are:",t
Count of character repetition in a string
word=str(raw_input('Enter the string:'))
n=raw_input('Enter the character to search in string:')
c=0
for i in word:
if i==n:
c=c+1
print 'Number of times the character repeated is', c
n=raw_input('Enter the character to search in string:')
c=0
for i in word:
if i==n:
c=c+1
print 'Number of times the character repeated is', c
Subscribe to:
Posts (Atom)