Replacing items within a Python list might seem like an advanced technique. However, Python’s syntax makes this task far easier than it would be within most other programming languages. However, there’s really not any single method that’s a definitive answer to to this task. As such, we’ll look at a few examples which demonstrate how to go about replacing items in a Python list. We’ll begin with a simple example that will work with a list of numbers and multiple items. This will show the simplest way to replace item in list Python.
# replace item in list python numList = [5, 9, 3, 6, 83, 4] numList= numList + 10 print(numList)
In this example we start out with a given list of integers – numList. For the next step, keep in mind that a Python program begins indexing with 0 instead of one. So when we select the third number in the given list with a specific index of numList. We can then perform a simple action with that index position variable to show that it’s usable before we move to list indexing multiple elements. In this case we’ll add the integer in numList to 10. This value is then assigned to numlist where it will replace the original value.
Finally, we can print the full list or sublist and see that the index value in numList has been increased by 10. This Python code shows how we can manipulate string values within a list element. We’ll begin by modeling something more akin to a real world problem. Imagine that we were writing some text only to discover that a faulty keyboard had been inputting a ‘z’ instead of an ‘a’. No need to fear, because we could fix that quite easily with a small Python script.
strList = ["zll", "znimals", "like", "zpples"] strlistNew =  for string in strList: newStrlist = string.replace("z", "a") strlistNew.append(newStrlist) print("The Original list of: ") print(strList) print("has been changed to: ") print(strlistNew)
We begin with a list of string values. In this example, we’ll keep the original string for later comparison. Because we want to keep those initial values we’ll create a new list called strlistNew and keep the existing list as strList. We’ll then begin going over the Python list elements in a loop. Each element, now stored in string, will have every instance of “z” replaced by “a”. Note that this would work on multiple values if the loop encountered them.
Now let’s try using list comprehension to replace a new element. This is a lot more concise than the typical loop method. Keep in mind that this example is a little longer than it needs to be. Normally we wouldn’t be creating a variable specifically to modify or printing it out afterward. This means that we’d essentially be able to perform a fairly complex replacement in just a single line of code.
originalText = 'The fun of drython programming'.split() newText = ['python' if ot == "drython" else ot for ot in originalText] print(newText)
In this list indexing method python tutorial example we begin by splitting a string into its component elements. The split function defaults to splitting strings on whitespace elements. As such, the original list will contain every word of the original sentence as a separate new element, even if there is a negative value or other non regular expressions. We’ll then sort through the original list looking for instances of “drython”. If found, “drython” will be changed to Python. The end result is assigned to newText. Though we could of course fully replace originalText if desired.
The only downside to this method of working with lists is that it can become somewhat complex as we work with longer elements. For example, a nested list made up of multiple lists or sublist can be a little difficult to follow within this list indexing method. However, we’re aided by the fact that a Python program is generally lax about each list item or list element within a specific index list. This gives us a lot more freedom in how we manipulate our python list elements than if we were using an array. As such we can generally sort and replace elements within a given index list in any way that suits our individual programming style.