airzinger1
airzinger1

Reputation: 161

How can I match items from one list to the other in python?

I have a list called users2 which is a master list of users. I have another list called codes has codes for each user in users2. The codes for the associated users are in the same order as in users2. So for example the code for bil is 56 because it is the second item in users2 and the second item in codes.

I also have another list called users1 that has some of the names from users2. I want to output of list of the associated codes for users1 based what the codes are for the same users in users2. So if the first item in user1 is bil, I want the first item in my output list to be 56.If users1 has a user name that is not in users2, the index in the output code list should be blank or "". I need this in python.

Here is what I have:

users1=['bil','kim','john''tim']
users2['jim','bil','kim','tim''mike']
codes=['12''56','10','34','67']


for i in  range(len(users2)):
    for j in range(len (users1)):
        if users2[i]  == users1[j]:
            ordered.append(zipcodes[j])
print ordered

Upvotes: 3

Views: 73

Answers (4)

Coolfrog
Coolfrog

Reputation: 1

You can try using dictionaries to store the users2 list and the codes list as a key:value pair. Then run a comparison between the users1 list and this list and print out the codes.

code:

    user2=dict()
    users2 = {'jim':'12','bil':'56','kim':'10','tim':'34','mike':'67'}
    users1=["bil",'kim','john','tim']
    ordered=list()

for i in users1:
 for j in users2:
    if i==j:
     ordered.append(users2[j])
 if i not in users2:
    ordered.append('')
print ordered

output:

    ['56','10','','34']

Upvotes: 0

RoadRunner
RoadRunner

Reputation: 26335

Just keep it nice and simple. A list and dict with a couple of loops solves the problem.

users1=['bil','kim','john','tim']
users2 = ['jim','bil','kim','tim','mike']
codes=['12','56','10','34','67']

name_codes = {}
for key, value in zip(users2, codes):
    name_codes[key] = value

result = []
for user1 in users1:
    for user2 in name_codes:
        if user1 == user2:
            result.append(name_codes[user2])
    if user1 not in name_codes:
        result.append('')
print(result)

Output:

['56', '10', '', '34']

Update:

I fixed you're original code:

ordered = []
for user in users1:
    for i in range(len(users2)):
        if user == users2[i]:
            ordered.append(codes[i])
    if user not in users2:
        ordered.append('')
print(ordered)

Output:

['56', '10', '', '34']

Upvotes: 1

user6451264
user6451264

Reputation:

You are probably using the wrong data structure. You want users2 and codes to be combined into a dictionary.

users1=['bil','kim','john','tim']
users2 = ['jim','bil','kim','tim','mike']
codes=['12','56','10','34','67']

users2_codes = dict(zip(users2, codes))
users1_codes = [users2_codes[user] if user in users2_codes else '' for user in users1 ]
print(users1_codes)

#>>> ['56', '10', '', '34']

Upvotes: 2

akuiper
akuiper

Reputation: 215117

You can construct a dictionary from users2 and codes and then loop through users1 and look up:

[dict(zip(users2, codes)).get(u) for u in users1]
# ['56', '10', None, '34']

You can specify a different value for the missing user in the get method of dictionary:

[dict(zip(users2, codes)).get(u, "") for u in users1]
# ['56', '10', '', '34']

Upvotes: 1

Related Questions