2

I have a map for object IDs:

mapping = {'id-1':'id-10', 'id-2':'id-14', 'id-3':'id-19'}

And two lists which contains objects, the id is an attribute of the object, such as object_id1.id = 'id-1'.

list1 = [object_id1, object_id3, object_id4, object_id5]
list2 = [object_id19, object_id11, object_id14, object_id10]

I want to have a one list with those related object in a set. Such as

output = [(object_id1, object_id10), (object_id2, object_id14),(object_id3, object_id19)]

Is that possible to do it in one single for loop?

Selcuk
  • 57,004
  • 12
  • 102
  • 110
  • 6
    Do you want to convert your `map` dict to a list of tuples? Try `list(map.items())`. BTW, try refraining from making a variable name same as built-in functions; it will shadow them. – Chris Jan 02 '20 at 01:46
  • 1
    What is the class definition for those "object"s? Do they have an attribute that gives you the id? A list doesn't know the variable names you assigned to the elements in it. – Selcuk Jan 02 '20 at 01:53
  • 1
    @Selcuk: the id is an attribute of the object, such as object_id1.id = 'id-1'. – MadaManu Jan 02 '20 at 01:54
  • Yes there is an attribute gives the object id. And I want those two objects merged together.. – SentimentalK Jan 02 '20 at 01:56

2 Answers2

7

You can't do it in a single for loop, but you can do it in two (not nested) loops:

>>> mapping = {'id-1': 'id-10', 'id-2': 'id-14', 'id-3': 'id-19'}
>>> list1 = [object_id1, object_id3, object_id4, object_id5]
>>> list2 = [object_id19, object_id11, object_id14, object_id10]

construct a lookup dict for finding items by id in list2:

>>> lookup_dict = {item.id: item for item in list2}

then you can simply do:

>>> output = [(item, lookup_dict[mapping[item.id]]) for item in list1 if mapping[item.id] in lookup_dict]
Selcuk
  • 57,004
  • 12
  • 102
  • 110
0

Edit: Fixed up my misunderstanding of the problem, This is partially adapted from: Searching a list of objects in Python

Assuming no duplicates between list1 and list2, as well as always finding a match

This might work for this, I havent tested this so you might find a few syntax errors possibly:

for item in map:
   xResult = [x for x in list1 if x.id == item or x.id == map[item]][0]
   yResult = [y for y in list2 if y.id == item or y.id == map[item]][0]
   if item == xResult:
       firstTupleItem = xResult
       secondTupleItem = yResult
   else:
       firstTupleItem = yResult
       secondTupleItem = xResult
   tuple = (firstTupleItem,secondTupleItem)
   output.append(tuple)

of course this could be much optimized or improved, but it could be a reasonable starting point

Edit: Selcuk's answer is more ideal but I will keep this up for reference's sake

Zaid Al Shattle
  • 1,454
  • 1
  • 12
  • 21
  • Oops, meant to write item.Value – Zaid Al Shattle Jan 02 '20 at 02:07
  • Isnt the map a dictionary? Thus item is a dictionary item? – Zaid Al Shattle Jan 02 '20 at 02:08
  • You should learn more about Python before posting answers. When you iterate over a dictionary you get the keys, which are strings in this case. Also there is no such thing as a "dictionary item" hence there are no attributes as `.Key` or `.Value`. You are possibly confusing it with another programming language. – Selcuk Jan 02 '20 at 02:09
  • 1
    Mine was a suggestion, not a request. Feel free to post anything. – Selcuk Jan 02 '20 at 02:13