Iterate over dict values

If I would like to iterate over the dictionary values ​​that are stored in a tuple.

I need to return an object that holds the value "CI", I assume that I need some kind of for loop:

z = {'x':(123,SE,2,1),'z':(124,CI,1,1)} for i, k in db.z: for k in db.z[i]: if k == 'CI': return db.z[k] 

Perhaps something is missing here, the reference point will be good.

if there is a faster way to make it all help a lot

+5
source share
4 answers
 z = {'x':(123,"SE",2,1),'q':(124,"CI",1,1)} for i in z.keys(): #reaching the keys of dict for x in z[i]: #reaching every element in tuples if x=="CI": #if match found.. print ("{} holding {}.".format(i,x)) #printing it.. 

This may solve your problem.

Output:

 >>> q holding CI. >>> 

Edit for your comment:

 def func(*args): mylist=[] z = {'x':(123,"SE",2,1),'q':(124,"CI",1,1)} for x,y in z.items(): for t in args: if t in y: mylist.append(x) return mylist print (func(1,"CI")) 

Output:

 >>> ['q', 'q', 'x'] >>> 

Hope this is what you want, otherwise the first method already prints all the keys, for example the output:

 if x==1 or x=="CI": >>> x holding 1. q holding CI. q holding 1. q holding 1. >>> 
+10
source

Dictionary Iteration Ways

First of all, there are several ways you can loop a dictionary.

The cycle directly above the dictionary:

 >>> z = {'x':(123,'SE',2,1),'z':(124,'CI',1,1)} >>> for key in z: ... print key, ... 'x' 'z' 

Note that the loop variables that are returned when you simply iterate over the dictionary are the keys, not the values ​​associated with these keys.

Quoting from dictionary values:

 >>> z = {'x':(123,'SE',2,1),'z':(124,'CI',1,1)} >>> for value in z.values(): # Alternatively itervalues() for memory-efficiency (but ugly) ... print value, ... (123,'SE',2,1) (124,'CI',1,1) 

Cycle and keys and values:

 >>> z = {'x':(123,'SE',2,1),'z':(124,'CI',1,1)} >>> for key, value in z.items(): # Again, iteritems() for memory-efficiency ... print key, value, ... 'x' (123,'SE',2,1) 'z' (124,'CI',1,1) 

The last two are somewhat more efficient than looping through the keys and running z [key] to get the value. It is also possibly more readable.

Based on these ...

List of concepts

List of concepts . For a simple search case, only "CI":

 >>> z = {'x':(123,'SE',2,1),'z':(124,'CI',1,1)} >>> [key for key, value in z.items() if 'CI' in value] ['z'] 

To search for dict keys containing multiple search items:

 >>> z = {'x':(123,'SE',2,1),'z':(124,'CI',1,1)} >>> search_items = ('CI', 1) # Only keys that hold both CI and 1 will match >>> [key for key, value in z.items() if all(item in value for item in search_items)] ['z'] 

To search for dict keys containing any of several search items:

 >>> z = {'x':(123,'SE',2,1),'z':(124,'CI',1,1)} >>> search_items = ('CI', 'SE', 'JP') # Keys that hold any of the three items will match >>> [key for key, value in z.items() if any(item in value for item in search_items)] ['x', 'z'] 

If the last two look too complex as single-line, you can rewrite the last bit as a separate function.

 >>> z = {'x':(123,'SE',2,1),'z':(124,'CI',1,1)} >>> search_items = ('CI', 'SE', 'JP') # Keys that hold any of the three items will match >>> def match_any(dict_value, search_items): ... return any(item in dict_value for item in search_items) ... >>> [key for key, value in z.items() if match_any(value, search_items)] ['x', 'z'] 

Once you get used to the syntax [x for x in iterable if condition (x)], the format should be very easy to read and follow.

+18
source

try the following:

 >>> z = {'x':(123,'SE',2,1),'z':(124,'CI',1,1)} >>> list(filter(lambda x:'CI' in z.get(x),z)) ['z'] 
+2
source
 z = {'x':(123,"SE",2,1),'q':(124,"CI",1,1)} for key, val in z.items(): if 'CI' in val: return z[key] 
0
source

Source: https://habr.com/ru/post/980413/


All Articles