2

I have pairs of 4 lists a and b with integer values such as list_1a = [1,2,3,...] and list_1b = [8,11,15,...]. The idea is that the integer values in list_1a are now represented by the integer values in list_1b, and the same for list_2a and list_2b etc.

Now I have a list of 4 columns final_list which contained integer values corresponding to the a lists. I want to map the values in final_list to the values in the b lists. What is the quickest way to do this in python ?

Is there a quicker way than using lists ?

Edit:

To clarify the question, take the following example:

list_1a = [1,2,3]
list_1b = [8,11,15] 

list_2a = [5,6,7,8]
list_2b = [22,26,30,34]

list_3a = [11,12,13,14,18]
list_3b = [18,12,25,28,30]

list_4a = [51,61,72,82]
list_4b = [73,76,72,94]
  • Note that some of these lists can contain more than a million entries (So maybe memory can be an issue)
  • The lists do not have the same length
  • All of the integer values in these lists are unique to their lists, i.e. list_1a + list_1b will never have a repeating integer value.

final_list should look like final_list_b after the mapping occurs

final_list_a = [[1,6,11,51],[3,6,14,72]]
final_list_b = [[8,26,18,73],[15,26,28,72]]

To put things into perspective, this questions is for a database application where these "lists" contain auto-generated key values

AnarKi
  • 857
  • 1
  • 7
  • 27
  • What have you tried so far? – Rachit kapadia May 07 '18 at 10:07
  • 1
    Can you please add some code snippet as to what will be the final list that you are looking for? – abhinav May 07 '18 at 10:08
  • 1
    I'd probably use `zip` and `dict` for this. If you want more specific advice, you should post a [mcve], complete with some short sample lists, so we can get a better idea of what you're actually doing. – PM 2Ring May 07 '18 at 10:11

1 Answers1

5

I think what you want is a dictionary, which associates keys with values. Unless i'm confused about what you want to do here.

So if I make 4 short example lists.

list_1a = [1,2,3,4]
list_1b = [8,11,15,18]
list_2a = [5,6,7,8]
list_2b = [22,26,30,34]

and make them into a big list of all "a" values and all "b" values.

a_list = list_1a + list_2a
b_list = list_1b + list_2b

I can then use zip to merge the lists into a dictionary

my_dict = dict(zip(a_list, b_list))

print(my_dict)

See: how to merge 2 list as a key value pair in python

for some other ways to do this last bit.

result:

{1: 8, 2: 11, 3: 15, 4: 18, 5: 22, 6: 26, 7: 30, 8: 34}

Now your "a" list makes up the keys of this dictionary.. while the "b" list make up the values. You can access the values by using the keys. here's some examples.

print(my_dict.keys())
print(my_dict.values())
print(my_dict[5])

gives me:

[1, 2, 3, 4, 5, 6, 7, 8]
[8, 11, 15, 18, 22, 26, 30, 34]
22

Is this what you want?

EDIT: I feel that I should note that while my dictionary has printed in order, dictionaries are actually not ordered like lists. You might want to look into collections.OrderedDict or sorted if this is important to you.

Update:

For what you want to do, maybe consider nested dictionaries. You can make a dictionary whose values are dictionaries, also note that when 1a and 1b don't match in length, zip doesn't care and just excludes 60:

list_1a = [1,2,3,4]
list_1b = [8,11,15,18,60]
list_2a = [5,6,7,8]
list_2b = [22,26,30,34]

a_dict = dict(zip(list_1a, list_2a))
b_dict = dict(zip(list_1b, list_2b))

my_dict = {"a" : a_dict, "b" : b_dict}

print(my_dict)

Result:

{'a': {1: 5, 2: 6, 3: 7, 4: 8}, 'b': {8: 22, 18: 34, 11: 26, 15: 30}}

Now you can access the inner values in a different way:

print(my_dict["a"].keys())
print(my_dict["a"].values())
print(my_dict["a"][4])

Result:

[1, 2, 3, 4]
[5, 6, 7, 8]
8
Zhenhir
  • 1,157
  • 8
  • 13
  • 1
    This is close to what I am looking for. However how would this scale memory wise ? Some of those lists have 10million entries... – AnarKi May 07 '18 at 10:53
  • 1
    I've used dicts with large amounts of data and they scale very well. If we're talking something of that size it might be a good idea to create a dict in the first place instead of merging lists, If that's at all possible. dicts are the only iterable that can do what you want, lists are very inefficient when they get large. You can't really use sets because the keys wouldn't line up with the values. – Zhenhir May 07 '18 at 11:09
  • See [here](https://stackoverflow.com/questions/513882/python-list-vs-dict-for-look-up-table) for more details on the efficiency of iterables. Seems dicts are good for speed, just like sets.. but pretty hard on memory. I still don't see a reasonable alternative for you. – Zhenhir May 07 '18 at 11:14
  • 1
    Can you check the edited version of the question ? there will be an issue since I want to leave the values of each pair of lists, i.e list_1a and list_1b, list_2a and list_2b etc. separate since they are unique to their lists – AnarKi May 07 '18 at 11:24
  • Ok, I edited in an example of nested dicts. You can't make a set of dicts because they're unhashable. You also have the option of making a list of dicts if you want. – Zhenhir May 07 '18 at 11:36