Program 4
Program 4
TITLE : Program to remove the ith occurrence of the given word in a list where words repeat.
Problem Description : The program takes a list and removes the ith occurrence of the given
word in the list where words can repeat.
Theory / Analysis
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or
a string).
This is less like the for keyword in other programming languages, and works more like an
iterator method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
Example
for x in fruits:
print(x)
Example
for x in "banana":
print(x)
With the break statement we can stop the loop before it has looped through all the items:
Example
for x in fruits:
print(x)
if x == "banana":
break
To loop through a set of code a specified number of times, we can use the range() function,
The range() function returns a sequence of numbers, starting from 0 by default, and increments
by 1 (by default), and ends at a specified number.
Example
for x in range(6):
print(x)
The range() function defaults to 0 as a starting value, however it is possible to specify the starting
value by adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6):
Example
print(x)
The else keyword in a for loop specifies a block of code to be executed when the loop is
finished:
Example
Print all numbers from 0 to 5, and print a message when the loop has ended:
for x in range(6):
print(x)
else:
print("Finally finished!")
The else block will NOT be executed if the loop is stopped by a break statement.
Example
Break the loop when x is 3, and see what happens with the else block:
for x in range(6):
if x == 3: break
print(x)
else:
print("Finally finished!")
Nested Loops
The "inner loop" will be executed one time for each iteration of the "outer loop":
Example
for x in adj:
for y in fruits:
print(x, y)
Equals: a == b
Not Equals: a != b
These conditions can be used in several ways, most commonly in "if statements" and loops.
Example
If statement:
a = 33
b = 200
if b > a:
Indentation
Python relies on indentation (whitespace at the beginning of a line) to define scope in the code.
Other programming languages often use curly-brackets for this purpose.
Example
a = 33
b = 200
if b > a:
Elif
The elif keyword is pythons way of saying "if the previous conditions were not true, then try this
condition".
Example
a = 33
b = 33
if b > a:
elif a == b:
Else
The else keyword catches anything which isn't caught by the preceding conditions.
Example
a = 200
b = 33
if b > a:
elif a == b:
else:
Program to remove the ith occurrence of the given word in a list where words repeat.
2. Accept the values into the list using a for loop and insert them into the list.
4. Then use an if statement to check if the word to be removed matches the element and the
occurrence number and otherwise it appends the element to another list.
5. The number of repetitions along with the updated list and distinct elements is printed.
6. Exit.
Program
Here is source code of the Python Program to remove the ith occurrence of the given word in list
where words can repeat. The program output is also shown below.
a=[]
for x in range(0,n):
a.append(element)
print(a)
c=[]
count=0
for i in a:
if(i==b):
count=count+1
if(count!=n):
c.append(i)
else:
c.append(i)
if(count==0):
else:
Enter element1:"apple"
Enter element2:"apple"
Enter element3:"ball"
Enter element4:"ball"
Enter element5:"cat"