jeudi 10 août 2017

map if condition and append to list

Is there an easy way to code the following example by using map function? If not, how could be done by using list comprehension?

lst1=['a','b','c']
lst2=['a','b','c','d']

l=[]
for iteml1 in lst1:
        if iteml1 in lst2:
            l.append((lst1.index(iteml1),iteml1))

l is a vector that contains the index of lst1 element in lst2 list and lst2 element. output:

l
Out[1]: [(0,'a'),(1,'b'),(2,'c')]

Aucun commentaire:

Enregistrer un commentaire