user3349070
user3349070

Reputation: 27

Striping attributes in compound list using python

When I execute the following code,

department=[]
for dpmt in departments:
    department.append(dpmt)
print department

the outcome comes out as follows:

[('d009', 'Customer Service'), ('d005', 'Development'), ('d002', 'Finance'), ('d003', 'Human Resources'), ('d001', 'Marketing'), ('d004', 'Production'), ('d006', 'Quality Management'), ('d008', 'Research'), ('d007', 'Sales')]

I would like to take out the leading character, 'd', in all attributes. I've tried this but it doens't work

department.sort()
for id, dpmt in department:
    id=id.lstrip('d')

This prints out the same outcome

[('d001', 'Marketing'),
 ('d002', 'Finance'),
 ('d003', 'Human Resources'),
 ('d004', 'Production'),
 ('d005', 'Development'),
 ('d006', 'Quality Management'),
 ('d007', 'Sales'),
 ('d008', 'Research'),
 ('d009', 'Customer Service')]

What am I doing wrong?

Upvotes: 0

Views: 38

Answers (2)

Navith
Navith

Reputation: 1069

You're only assigning id.lstrip("d") to the name id (by the way, don't use names shared by builtins), so the change is not reflected in the list.

department[:] = [(id_.lstrip("d"), dpmt) for (id_, dpmt) in sorted(departments)]

Upvotes: 1

iced
iced

Reputation: 1572

Simple explanation: you are changing copies of "keys".

department = map(lambda d: (d[0].lstrip('d'), d[1]), department)

Try something like this.

Upvotes: 0

Related Questions