# Replacing values in two different size list

Page 1 of 1

## 8 Replies - 731 Views - Last Post: 04 February 2020 - 02:11 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=418335&amp;s=33298448838326b5c389f04d45b95379&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 2Dabz

Reputation: 4
• Posts: 100
• Joined: 10-September 17

# Replacing values in two different size list

Posted 31 January 2020 - 11:13 AM

I have the list f3 = ['a', 'a', 'b', 'b', 'c', 'c', 'a', 'b', 'b']

I put the list in order of frequency f1 = ['b', 'a', 'c']

Now f1 converts to f2 = ['e', 't', 'a']

So now I want to take the converted values from f2 and swap them back on f3 and wind up with

f4 = ['t', 't', 'e', 'e', 'a', 'a', 't', 'e', 'e']

I can't figure out how to produce f4. I've tried if and for statements but can't come up with anything

that works. Any thoughts? Thanks.

Is This A Good Question/Topic? 1

## Replies To: Replacing values in two different size list

### #2 modi123_1

• Suitor #2

Reputation: 15503
• Posts: 62,075
• Joined: 12-June 08

## Re: Replacing values in two different size list

Posted 31 January 2020 - 01:22 PM

If I am following, you could just a 'replace' to search for some index-variable tracked value in f1 with the same index value in f2.

### #3 DK3250

• Pythonian

Reputation: 566
• Posts: 1,804
• Joined: 27-December 13

## Re: Replacing values in two different size list

Posted 31 January 2020 - 02:16 PM

Let me hint you:
You can use two nested loops, each using the enumerate function.
My working code is only 4 lines, so difficult to hint more without actually disclose finished code.

This post has been edited by DK3250: 31 January 2020 - 02:18 PM

### #4 2Dabz

Reputation: 4
• Posts: 100
• Joined: 10-September 17

## Re: Replacing values in two different size list

Posted 31 January 2020 - 09:00 PM

I've been playing around with it but no luck yet.

### #5 DK3250

• Pythonian

Reputation: 566
• Posts: 1,804
• Joined: 27-December 13

## Re: Replacing values in two different size list

Posted 01 February 2020 - 01:18 AM

ok, it's such a small code, let's go through it.
First using only lists:
```f3 = ['a', 'a', 'b', 'b', 'c', 'c', 'a', 'b', 'b']
f1 = ['b', 'a', 'c']
f2 = ['e', 't', 'a']

f4 = []  # make an empty list
for item in f3:  # iterate through f3
for n1, letter in enumerate(f1):  # iterate through f1 - and get index number
if item == letter:
f4.append(f2[n1])  # add to the result list using the f1-index in combination with f2
print(f4)
```

Next, using a dictionary - this is more efficient and probably also more pythonic:
```f3 = ['a', 'a', 'b', 'b', 'c', 'c', 'a', 'b', 'b']
f1 = ['b', 'a', 'c']
f2 = ['e', 't', 'a']

d = dict(zip(f1, f2))  # make a dict linking f1 and f2
f4 = []  # make an empty list
for item in f3:  # iterate through f3
f4.append(d[item])  # add to result list using the dict
print(f4)

```

I hope you get it - feel free to ask again.

### #6 2Dabz

Reputation: 4
• Posts: 100
• Joined: 10-September 17

## Re: Replacing values in two different size list

Posted 02 February 2020 - 11:26 AM

Your code works. Thanks. I am still having a problem implementing in on mine. I get a KeyError: 'b' from the line f4.append(d[items]).

Here is how I am generating the list that contains 'b' in f1.

```f1 = [key for key, _ in frequency.most_common()]

```

This gives me the fist f1 = ['b', 'a', 'c'].

I'm thinking this isn't making a normal list?

### #7 2Dabz

Reputation: 4
• Posts: 100
• Joined: 10-September 17

## Re: Replacing values in two different size list

Posted 02 February 2020 - 11:51 AM

Figured it out. When I was creating f2 I was doing something that forced the KeyError. Switched up the way the list is generated and it works now. Thanks again.

### #8 DK3250

• Pythonian

Reputation: 566
• Posts: 1,804
• Joined: 27-December 13

## Re: Replacing values in two different size list

Posted 02 February 2020 - 01:22 PM

Glad I could help.
But assistance without conveying new understanding is really not helpful.
So, just for my peace of mind: Are you sure you understand the solution in detail?

### #9 2Dabz

Reputation: 4
• Posts: 100
• Joined: 10-September 17

## Re: Replacing values in two different size list

Posted 04 February 2020 - 02:11 PM

Yes. Your documentation is good. I knew what I wanted to do just not the code to get there. The zip and dict functions made it really easy. Link the values in two list. Then run through original list and replace matching value with the linked value.