Try using a list comprehension and a conditional expression.
>>> a=[1,2,3,1,3,2,1,1]
>>> [4 if x==1 else x for x in a]
[4, 2, 3, 4, 3, 2, 4, 4]
More Related Contents:
- Replace values in list using Python [duplicate]
- Removing a list of characters in string
- Replace special characters in a string in Python
- TypeError: ‘float’ object is not subscriptable
- How to replace elements in a list using dictionary lookup
- Is there a way to ordinate numbers in a list?
- finding the biggest sequence of repeating numbers in a list
- How to append new line to the list
- Sort a list of tuples by 2nd item (integer value) [duplicate]
- Convert [key1,val1,key2,val2] to a dict?
- Filtering a list of strings based on contents
- Convert tuple to list and back
- Unpacking a list / tuple of pairs into two lists / tuples
- Python 3 turn range to a list
- Call int() function on every list element?
- Split list into smaller lists (split in half)
- Find nearest indices for one array against all values in another array – Python / NumPy
- Splitting on last delimiter in Python string?
- How to replace negative numbers in Pandas Data Frame by zero
- Transform “list of tuples” into a flat list or a matrix
- Replace first occurrence only of a string?
- Inserting a value into all possible locations in a list
- Make dictionary from list with python [duplicate]
- Find the index of an item in a list of lists
- Slicing list of lists in Python
- Replacing a sublist with another sublist in python
- How to get the cartesian product of multiple lists
- Union find implementation using Python
- Counting depth or the deepest level a nested list goes to
- Which is the most efficient way to iterate through a list in python?