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

py 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 :: install local package python 
Python :: join to dataframes pandas 
Python :: how append a directory based on current directory python 
Python :: python text color 
Python :: .describe() python 
Python :: numpy method to make polynomial model 
Python :: relative import in python 
Python :: python list add element to front 
Python :: making gifs via python 
Python :: django url static 
Python :: adding text cv2 
Python :: enter selenium in python 
Python :: for python 
Python :: pip not downlaoding cryptography wheel macos 
Python :: python autocorrelation plot 
Python :: type de variable python 
Python :: month name in python 
Python :: compose functions python 
Python :: get dict values in list python 
Python :: pandas add value to excel column and save 
Python :: regex name extract 
Python :: python input float 
Python :: python verify if string is a float 
Python :: select multiple dict 
Python :: querydict instance is immutable 
Python :: how to print specific part of a dictionary in python 
Python :: pytplot arc 
Python :: add new row to dataframe pandas 
Python :: python string to lower 
Python :: np.reshape() 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =