Search code examples
pythonarraysstringliststring-comparison

Want get value of other list where value matched


I have two lists which are following.

characteristic  = ['length', 'width', 'height', 'Thread length', 'space']
value = ['length 34','width ab23','Thread length 8ah','space','height 099']

I have written a loop.

temp_str = {}



for x in characteristic:

  for z in value:  

      if x in z:

        temp_str = z.replace(x,'')

        temp_str += ','

        print(temp_str)

I am getting output:

34,
Thread  8ah,
 ab23,
 099,
 8ah,
,

but in real I want output like following

34,ab23,099,8ah, 

Solution

  • Try the following:

    characteristic  = ['length', 'width', 'height', 'Thread length', 'space']
    value = ['length 34','width ab23','Thread length 8ah','space','height 099']
    temp_str = ''
    
    for x in characteristic:
        for z in value:  
            if z.startswith(x) and z!=x:
                temp_str += z.split(' ')[-1]
                temp_str += ','
    print(temp_str)
    

    Output:

    34,ab23,099,8ah,
    

    Note: space was ignored because it was not included in your required output