Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading