Can we modify array in python?

I want to make a function that adds a specific word in front of every string in the array. At the end I want the array changed. I have this code:

def make_great(magicians):
    """Change magicians"""
    for magician in magicians:
        magician = "the Great" + magician


magicians = ["hudini", "angel", "teller", "anderson", "copperfield"]
make_great(magicians)
print(magicians)

This code doesn't change the array. How can I make my function work?

asked Jun 28, 2016 at 19:11

Martin DimitrovMartin Dimitrov

3491 gold badge2 silver badges11 bronze badges

0

You can use enumerate to loop over the list with both the index and the value, then use the index to change the value directly into the list:

def make_great(magicians):
    for index, magician in enumerate(magicians):
        magicians[index] = "the Great " + magician

answered Jun 28, 2016 at 19:14

Can we modify array in python?

0

When you use a for-each loop:

def make_great(magicians):    
    for magician in magicians:
        magician = "The Great" + magician

you're actually creating a new string magician; so modifying it won't modify the original array as you found.

Instead, iterate over the items in the array:

def make_great(magicians):    
    for i in range(len(magicians)):
        magicians[i] = "The Great" + magicians[i]

Or use an enumerator as proposed above. See: How to modify list entries during for loop?

answered Jun 28, 2016 at 19:18

CheckmateCheckmate

9737 silver badges16 bronze badges

enumerate would be the best thing to do in this case, then modify the value at each index in the array.

for i, magician in enumerate(magicians):
    magicians[i] = "the Great " + magician

answered Jun 28, 2016 at 19:15

RyanRyan

14.1k8 gold badges60 silver badges102 bronze badges

1

You can do this with a little Python magic combining lambda and map together

>>> magicians = ['hudini', 'angel', 'teller', 'anderson', 'copperfield']
>>> map(lambda el:'pre_'+el,magicians)
['pre_hudini', 'pre_angel', 'pre_teller', 'pre_anderson', 'pre_copperfield']

Try it out here

answered Jun 28, 2016 at 19:23

loretoparisiloretoparisi

14.8k11 gold badges93 silver badges132 bronze badges

Python >> Arrays >> Modify-elements-in-an-array

Question:

Write a python program that modifies elements in an array.

Solution:

Here is a python example that replaces elements in an array:

Source: (example.py)

arr = [10, 20, 30, 40, 50]
print(arr)
 
# replace the element indexed at 2 with 99
arr[2] = 99
print(arr)
 

Output:

$ python example.py
[10, 20, 30, 40, 50]
[10, 20, 99, 40, 50]

Looking to modify an item within a list in Python?

If so, you’ll see the steps to accomplish this goal using a simple example.

Step 1: Create a List

To start, create a list in Python. For demonstration purposes, the following list of names will be created:

Names = ['Jon', 'Bill', 'Maria', 'Jenny', 'Jack']
print(Names)

Run the code in Python, and you’ll get this list:

['Jon', 'Bill', 'Maria', 'Jenny', 'Jack']

Step 2: Modify an Item within the list

You can modify an item within a list in Python by referring to the item’s index.

What does it mean an “item’s index”?

Each item within a list has an index number associated with that item (starting from zero). So the first item has an index of 0, the second item has an index of 1, the third item has an index of 2, and so on.

In our example:

  • The first item in the list is ‘Jon.’ This item has an index of 0
  • ‘Bill’ has an index of 1
  • ‘Maria’ has an index of 2
  • ‘Jenny’ has an index of 3
  • ‘Jack’ has an index of 4

Let’s say that you want to change the third item in the list from ‘Maria’ to ‘Mona.’ In that case, the third item in the list has an index of 2.

You can then use this template to modify an item within a list in Python:

ListName[Index of the item to be modified] = New value for the item

And for our example, you’ll need to add this syntax:

Names[2] = 'Mona'

So the complete Python code to change the third item from Maria to Mona is:

Names = ['Jon', 'Bill', 'Maria', 'Jenny', 'Jack']

#modify
Names[2] = 'Mona'

print(Names)

When you run the code, you’ll get the modified list with the new name:

['Jon', 'Bill', 'Mona', 'Jenny', 'Jack']

Change Multiple Items Within a List

What if you want to change multiple items within your list?

For example, what if you want to change the last 3 names in the original list:

  • From ‘Maria’ to ‘Mona’
  • From ‘Jenny’ to ‘Lina’
  • From ‘Jack’ to ‘Mark’

You can then specify the range of index values where the changes are required. For our example, the range of index values where changes are required is 2:5. So here is the code to change the last 3 names in the list:

Names = ['Jon', 'Bill', 'Maria', 'Jenny', 'Jack']

#modify
Names[2:5] = 'Mona','Lina','Mark'

print(Names)

You’ll now see the updated list with the 3 new names:

['Jon', 'Bill', 'Mona', 'Lina', 'Mark']

You can get the same same results by using Names[-3:] as below:

Names = ['Jon', 'Bill', 'Maria', 'Jenny', 'Jack']

#modify
Names[-3:] = 'Mona','Lina','Mark'

print(Names)

And as before, you’ll now see the updated list with the 3 new names:

['Jon', 'Bill', 'Mona', 'Lina', 'Mark']

Can you modify an array in Python?

We can make changes to an array in different ways. Some of these are as follows: Assignment operator to change or update an array. Append() method to add one element.

How do you modify an array?

You click the formula in the cell or formula bar and you can't change a thing. Array formulas are a special case, so do one of the following: If you've entered a single-cell array formula, select the cell, press F2, make your changes, and then press Ctrl+Shift+Enter..

Can arrays be modified?

So a final array means that the array variable which is actually a reference to an object, cannot be changed to refer to anything else, but the members of the array can be modified.

Can we replace change any value in an array in Python?

The replace() function is used to return a copy of the array of strings or the string, with all occurrences of the old substring replaced by the new substring. This function is very useful if you want to do some changes in the array elements, where you want to replace a substring with some new string value.