InsideDarkWeb.com

How to extract the extract specific key from an dictionaries?

hello i’m at the beginning with python and while I was programming I ran into this problem :

pidred = 4042629
pidblue = 4038785

variantList = {'40426296': '5', '40387858': '6', '40387857': '5.5', '40387856': '5', '40387854': '4', '40387852': '3'}

variant_keys = list(variantList)

I would like to extract every key of the list variant that contains pidblue, but I am stuck… how can I do it?
Thank you

Stack Overflow Asked by Sto Ned on November 15, 2021

2 Answers

2 Answers

Check if the item contains pidblue and appends it to contains_pidblue

contains_pidblue=[]
for item in variant_keys:
    if str(pidblue) in item:
        contains_pidblue.append(item)

Answered by aidan0626 on November 15, 2021

Use a list comprehension

strblue = str(pidblue)
blue_keys = [key for key in variantList if strblue in key]

Answered by Barmar on November 15, 2021

Add your own answers!

Related Questions

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.