3

How do I replace James's salary from 1000 to 1500 and print James's data?

data = [['Ben', 'Manager', 3000],
           ['James', 'Cleaner', 1000],
           ['Ken', 'Supervisor', 2000]]

for (name,appt,salary) in data:
    if name == 'James':
        salary = 1500
        print(linked_data[1]) 

Here's my current output:

['James', 'Cleaner', 1000]

Expected output:

['James', 'Cleaner', 1500]
Sociopath
  • 13,068
  • 19
  • 47
  • 75
James Boer
  • 321
  • 4
  • 9
  • 28
  • 5
    Possible duplicate of [Access item in a list of lists](https://stackoverflow.com/questions/18449360/access-item-in-a-list-of-lists) – dennlinger Jul 13 '18 at 05:39

11 Answers11

7

You need to find the index at which it occurs. Use enumerate.

for idx, (name,appt,salary) in enumerate(data):
    if name == 'James':
        # salary is at index 2 in the inner list
        data[idx][2] = 1500
        print(f"{name},{appt},{salary}")
kantal
  • 2,331
  • 2
  • 8
  • 15
jhuang
  • 766
  • 1
  • 9
  • 16
3

If you know the index then you can do:

data = [['Ben', 'Manager', 3000],
           ['James', 'Cleaner', 1000],
           ['Ken', 'Supervisor', 2000]]


data[1][2] = 1500

print(data[1])

output:

['James', 'Cleaner', 1500]
Sociopath
  • 13,068
  • 19
  • 47
  • 75
2

You are unpacking from the list in for loop and creating 3 variables name, appt and salary. With this variables it isn't possible to easily change your data structure. What you need is extract a list from data and access it by indexing:

from pprint import pprint

data = [['Ben', 'Manager', 3000],
           ['James', 'Cleaner', 1000],
           ['Ken', 'Supervisor', 2000]]

for d in data:
    if d[0] == 'James':
        d[2] = 1500
        print(d)  # prints only changed row

pprint(data)  # prints whole structure

Prints:

['James', 'Cleaner', 1500]
[['Ben', 'Manager', 3000],
 ['James', 'Cleaner', 1500],
 ['Ken', 'Supervisor', 2000]]
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
1

Right now you're trying to iterate over three values that don't actually make sense as parameters to the for loop. What you need to do is access one nested array at a time, check if that array's name is James, and if it is, you change the salary.

data = [['Ben', 'Manager', 3000],
        ['James', 'Cleaner', 1000],
        ['Ken', 'Supervisor', 2000]]

for (person) in data:
    if person[0] == 'James':
        # change index 1 (occupation) to 1500
        person[1] = 1500

print(data)

Output:

[['Ben', 'Manager', 3000],
 ['James', 'Cleaner', 1000],
 ['Ken', 'Supervisor', 2000]]
Ben Botvinick
  • 2,837
  • 3
  • 16
  • 41
1

You need to access the second sublist of the list and then the third element of the sublist to change it. Have a look at the code below:

data[1][2] = 1500
print(data)
Yash Ghorpade
  • 607
  • 1
  • 7
  • 16
0

I would propose using a dictionary to store your information, like this:

data = [['Ben', 'Manager', 3000],
        ['James', 'Cleaner', 1000],
        ['Ken', 'Supervisor', 2000]]

data_dict = {k: {'position': p, 'salary': s} for k, p, s in data}

data_dict['James']['salary'] = 1500

print(data_dict)

Output:

{'Ben': {'position': 'Manager', 'salary': 3000}, 'James': {'position': 'Cleaner', 'salary': 1500}, 'Ken': {'position': 'Supervisor', 'salary': 2000}}

If you need to convert it back to a list, just use this:

print([[k] + list(v.values()) for k, v in data_dict.items()])

Output:

[['Ben', 'Manager', 3000], ['James', 'Cleaner', 1500], ['Ken', 'Supervisor', 2000]]
Ashish Acharya
  • 3,349
  • 1
  • 16
  • 25
0

To replace the salary:

data[1][2] = 1500

To print the data:

print(data[1])
Farhan
  • 429
  • 2
  • 9
0

Variable salary is just a copy of the value from data. You should modify the original list instead of the copy. The function enumerate can generate the index (from 0) you need for changing the value inside the list.

data = [['Ben', 'Manager', 3000],
       ['James', 'Cleaner', 1000],
       ['Ken', 'Supervisor', 2000]]

for i, (name, appt, salary) in enumerate(data):
    if name == 'James':
        data[i][2] = 1500
        print(data[i])

The output: ['James', 'Cleaner', 1500]

Isaac
  • 1
0

I think using class to replace 2D list is better to do.

class Employee:
    def __init__(self, name, job, salary):
        self.name = name
        self.job = job
        self.salary = salary

    def __str__(self):

        return '[{}, {}, {}]'.format(self.name, self.job, self.salary)

    def __repr__(self):

        return '[{}, {}, {}]'.format(self.name, self.job, self.salary)

data = [Employee('James', 'Cleaner', 1000),
        Employee('Ben', 'Manager', 3000)]

print(data)

for employee in data:
   if employee.name == 'James':
    employee.salary = 1500
    print(employee)
print(data)

Output:

[[James, Cleaner, 1000], [Ben, Manager, 3000]]
[James, Cleaner, 1500]
[[James, Cleaner, 1500], [Ben, Manager, 3000]]
Cyrbuzz
  • 119
  • 1
  • 8
0

You can also Use This

data = [['Ben', 'Manager', 3000], ['James', 'Cleaner', 1000], ['Ken', 'Supervisor', 2000]]
change = 3000
name = 'Ben'
print([[x, y, change] if x == name else [x, y, z] for x, y, z in data])

So You get this Output: [['Ben', 'Manager', 3000], ['James', 'Cleaner', 1000], ['Ken', 'Supervisor', 2000]]

Om trivedi
  • 32
  • 3
0

If You Want To Use Simple For Loop Then

data = [['Ben', 'Manager', 3000], ['James', 'Cleaner', 1000], ['Ken', 'Supervisor', 2000]]
for x in data:
    if 'Ben' in x:
        x[-1] = 1500
print(data) 
#Output: [['Ben', 'Manager', 1500], ['James', 'Cleaner', 1000], ['Ken', 'Supervisor', 2000]]
Om trivedi
  • 32
  • 3