Frequently Asked Python Program 10: How To Remove Nth occurrence of the word from a List

preview_player
Показать описание
Topic : How To Remove Nth occurrence of the word from a List

#########################
Udemy Courses:
#########################

Manual Testing+Agile with Jira Tool
************************************

Selenium with Java+Cucumber
********************************

Selenium with Python & PyTest
********************************

Selenium with python using Robot framework
****************************************

API Testing(Postman, RestAssured & SoapUI)
*****************************************

Web & API Automation using Cypress with Javascript
********************************************

Playwright with Javascript
**************************

Jmeter-Performance Testing
************************

SDET Essencials(Full Stack QA)
*************************

Appium-Mobile Automation Testing
************************************

Java Collections
*****************

Python Programming
*********************

Cucumber BDD Framework
***************************

Protractor with Javascript
***************************

####################################
Youtube Playlists:
####################################

Manual Testing & Agile
***********************

SQL
*************************

linux & Shell Scripting
**********************

Java
**********************

Selenium With Java+Cucumber
********************************

Python
********************************

Selenium With Python,Pytest&Behave
***************************************

Selenium With Python Using Robert Framework
(Web&API Testing)
*************************************************

API Testing (Postman,SoapUi,&Rest Assured)
**********************************************

Mobile App Testing Appium
****************************

Performance Testing Jmeter
*******************************

Maven,Jenkins,Git,Github,CI/CD
*******************************

SQL,DB Testing&ETL,Bigdata
*******************************

JavaScript Based Automation Tools
********************************

Selector Hub Tools
********************

GraphQL
******************

Cypress API Testing
********************

Cypress Web Testing
**********************

Playwright with Javascipt
**************************

#PythonNthOccurrence
#ListManipulation
#RemoveNthWord
#PythonListOps
#StringManipulation
#CodingChallenge
#PythonTips
#ProgrammingSolutions
#AlgorithmPractice
#TechInterview
#CodingProblems
#ListModification
#PythonTricks
#CodeOptimization
#PythonSnippets
#NthWordRemoval
#ListHandling
#PythonArrays
#StringOperations
#ProblemSolving
#CodingInPython
#ListAlgorithms
#PythonCommunity
#LearnToCode
#AlgorithmDesign
#PythonFunctions
#SoftwareDevelopment
#ProgrammingInPython
#PythonHelp
#CodeExamples
#CodingWorld
#TechSolutions
#PythonTricks
#CodeTips
#CSProblems
#PythonLearning
#CodingJourney
#PythonLogic
#CodingInspiration
#AlgorithmTricks
#ProblemSolvingSkills
#PythonTeaching
#CodeMagic
#PythonExploration
#CodeSimplicity
#TechKnowhow
#PythonMastery
#CodeMentorship
#CodingGenius
#PythonChallenge
Рекомендации по теме
Комментарии
Автор

what if we need to delete last word in the list?. not possible because of --> len(list)-1

vedanthasm
Автор

easier method would be using inbuilt function count in list .if count>1 then remove that element from list. easy

gtaburn
Автор

How to do if string given like this .. str1 = "programinpythonprogram"
Here word "program" is occurs twice.

priyathiruvathuru
Автор

len(mylist)-1 is not correctly explained.

abhisheksrivastava
Автор

Sir directly we can the remove keyword

gopinadhvulcni
Автор

the code is not correct. nth occurence is there in the last part

shadow_self
Автор

Thank you for posting the series. Very useful.

A small correction might require in the program. If the list contains many "geeks", this will not work well.
We will be getting an "Index" exception.

Ex:
mylist = ["geeks", "for", "news", "geeks", "paper", "geeks", "lake", "geeks"]
print(mylist)
word = "geeks"
n = 2

count = 0
for key in range(0, len(mylist) -1):
if mylist[key] == word:
count = count + 1
if count == n:
del mylist[key]
print(mylist)

uybfkzo
Автор

I use the code but it will create an error if we have occurances more than 2, because of index out of range. I would prefer below method.

my_list_1 = ["geeks", "for ", "geeks", "geeks"]
def remove_duplicte(lst):
seen = set()
result = []
for iteam in my_list_1:
if iteam not in seen:
seen.add(iteam)
result.append(iteam)
return result

mohamedismail
Автор

You can use enumerate function here

And break statement to end the loop after deleting the nth occurrence

sasidharnaidu
Автор

Hello sir
can u expailn this program ...
Q is : Given a string, write a program to remove all the words with K length.
input is : Tea is good for you 3
expected output is : is good

shivamani
Автор

This one is not working. Please find the code below and also the output and suggest.

mylist = ['Geek', 'for', 'Geek']
word = 'Geek'
n = 2
count = 0
for i in range(0, len(mylist)-1):
if mylist[i] == word:
count += 1
if count == n:
del mylist[i]

print(mylist)


Output: ['Geek', 'for', 'Geek']

LearningBigdata
Автор

Nice video however there is one correction. Code would not work if n'th word is at last index.

SantoshKumar-kgsx
Автор

how to reverse a word in list:

arr = ["hello", "world"]
for i in arr:
if i == "world":
print(i[::-1])

arr = ["hello", "world"]
for i in range(len(arr)):
if i == 1:
word = arr[i][::-1]
print(word)

kvelez
Автор

l=["geeks", "for", "geeks"]
a=list(set(l))
print(a)

shivashankaramalakanti
Автор

This code is not removing 2nd geeks...
In the first example you have shown 2nd geeks should be removed but fails..
i/p: ["geeks", "for", "geeks"]
o/p: ["geeks", "for", "geeks"]---which is wrong..

digitalcreator
Автор

my_list = [1, 2, 3, 3, "geeks", "for", "geeks"]
my_list = list(set(my_list))
print(my_list)

jmgcheng
Автор

Remove words in list:

arr = ["hello", "world"]
for i in range(len(arr)):
if i == 1:
arr.remove(arr[i])
print(arr)

arr = ["hello", "world"]
for i in arr:
if i == "world":
arr.remove(i)
print(arr)

arr = ["hello", "world", "world"]
dictionary = {}
for i in arr:
if i in dictionary:
arr.remove(i)
dictionary[i] = dictionary.get(i, 0)
print(arr)

arr = ["hello", "world", "world"]
dictionary = {}

def counting(arr, dictionary, i):
if i >= len(arr):
return f"\nArray: {arr}\nDictionary: {dictionary}"

if arr[i] in dictionary:
dictionary[arr[i]] += 1
arr.remove(arr[i])
else:
dictionary[arr[i]] = 1
return counting(arr, dictionary, i+1)

print(counting(arr, dictionary, 0))

kvelez