Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

replace a string in a list

list = ['helloXXX', 'welcomeXXX', 'to999', 'SofthuntUUU']
replace = [list.replace('XXX', 'ZZZ') for list in list]
print(replace)
Comment

replace list

# Replace 'Koweit' To 'Sudan'
my_list = [ "Egypt", "Koweit", "Algeria", "Morocco", "Tunisia" ]
my_list[ 1 ] = "Sudan"
print( my_list )
Comment

python how to replace a string in a list

# Replace a particular item in a Python list
a_list = ['aple', 'orange', 'aple', 'banana', 'grape', 'aple']
for i in range(len(a_list)):
    if a_list[i] == 'aple':
        a_list[i] = 'apple'
print(a_list)
# Returns: ['apple', 'orange', 'apple', 'banana', 'grape', 'apple']
Comment

Replace an item in a python list

# Replace Values in a List using Slicing
  
# define list
l = ['Hardik', 'Rohit', 'Rahul', 'Virat', 'Pant']
  
# find the index of Rahul
i = l.index('Rahul')
  
# replace Rahul with Shikhar
l = l[:i]+['Shikhar']+l[i+1:]
  
# print list
print(l)
Comment

PREVIOUS NEXT
Code Example
Python :: python = align 
Python :: kmp algorithm 
Python :: how to create tkinter window 
Python :: python button tkinter change color 
Python :: Python sort list alpha 
Python :: #find the difference in days between two dates. 
Python :: split column values 
Python :: Subset data frame by date 
Python :: for loop 
Python :: how to create a string in python 
Python :: python destructure object 
Python :: how to find missing item in a list 
Python :: w=how to tell if decimal in python 
Python :: python set to list 
Python :: python - gropuby based on 2 variabels 
Python :: python list of size 
Python :: maximum count of replacements in python 
Python :: python minecraft server python gui 
Python :: custom pylatex command 
Python :: log in python 
Python :: how to make reportlab table header bold in python 
Python :: python puissance 
Python :: python vars 
Python :: pytesseract restrict char 
Python :: Chef in his Office codechef solution 
Python :: python list comprehension nested loop 
Python :: get path and name of file for open() 
Python :: Python update to beginning of dictionary 
Python :: install python 3 
Python :: how to make a bot send whatever you dm it into a server discord.py 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =