logo
down
shadow

Get dictionary key by value if value is list


Get dictionary key by value if value is list

By : SisterObvious
Date : November 21 2020, 07:38 AM
it fixes the issue So this is related to a shorter question posted here earlier: Get key by value in dictionary , Use a list comprehension with a conditional
code :
>>> mydict = {'george':[1,2,3],'amber':[18,19]}
>>> [i for i in mydict if 19 in mydict[i]]
['amber']
>>> filter(lambda x: 19 in x[1], mydict.items())
[('amber', [18, 19])]


Share : facebook icon twitter icon
map the dictionary and list of dictionary,finally update the list of dictionary after mapping

map the dictionary and list of dictionary,finally update the list of dictionary after mapping


By : pfieni
Date : March 29 2020, 07:55 AM
wish of those help Your code did not break up req_dict and thus the last print statement just prints out a list of one dictionary which is the req_dict. Try to replace the last print statement with the following:
code :
print [{key: value} for key, value in req_dict.iteritems()]
If I make a list from a dictionary and if I change a property from an object of the list, will it show on the dictionary

If I make a list from a dictionary and if I change a property from an object of the list, will it show on the dictionary


By : Colt Cook
Date : March 29 2020, 07:55 AM
With these it helps TestObject is a class - so it is a reference type. When you create the list from the dictionary you just pass references to the objects which live in the heap (you can think about references as links). You are not passing objects here. Even dictionary does not hold objects - it holds only references to objects. You can have many references which point to the same object. And you can modify object via any of references. But the object is only one.
Python nested dictionary and list concatnate dictionary keys with list values

Python nested dictionary and list concatnate dictionary keys with list values


By : hiko
Date : March 29 2020, 07:55 AM
hop of those help? I have a nested dict as follows:
code :
lst = {"A": ["1", "2", "3"], "B": ["2"], "C": ["1", "2"]}
result = ["{}.{}".format(key, val)
          for key, vals in lst.items()
          for val in vals]
print(result)
Group list of dictionary as list of list of dictionary based on multiple keys

Group list of dictionary as list of list of dictionary based on multiple keys


By : Mintu Sharma
Date : March 29 2020, 07:55 AM
wish helps you How to group list of dictionary as list of list of dictionary based on multiple key elements(gender & class)?
code :
import itertools
from itertools import groupby
lst=[{'name':'tom','roll_no':1234,'gender':'male','class':1},
     {'name':'sam','roll_no':1212,'gender':'male','class':1},
     {'name':'kavi','roll_no':1235,'gender':'female','class':2},
     {'name':'maha','roll_no':1211,'gender':'female','class':2}]
keyfunc = key=lambda x:(x['class'],x['gender'])
final_lst = [list(grp) for key, grp in itertools.groupby(sorted(lst, key=keyfunc),key=keyfunc)]
print(final_lst)
[[{'name': 'tom', 'class': 1, 'roll_no': 1234, 'gender': 'male'}, {'name': 'sam', 'class': 1, 'roll_no': 1212, 'gender': 'male'}], [{'name': 'kavi', 'class': 2, 'roll_no': 1235, 'gender': 'female'}, {'name': 'maha', 'class': 2, 'roll_no': 1211, 'gender': 'female'}]]
comparing list with a dictionary index and returning dictionary values based on original list order

comparing list with a dictionary index and returning dictionary values based on original list order


By : Kishan
Date : March 29 2020, 07:55 AM
Any of those help Yes, there is, just iterate over the list and fetch the values from the dictionary:
code :
lst =['a','b','c','d']
dct = {'d':'fourth' ,'a': 'first','b':'second','c':'third'}
result = [dct[i] for i in lst]

print(result)
['first', 'second', 'third', 'fourth']
result = []
for e in lst:
    result.append(dct[e])
lst =['a','b','c','d', 'f']
dct = {'d':'fourth' ,'a': 'first','b':'second','c':'third'}
result = [dct.get(e, 'missing') for e in lst]
print(result)
['first', 'second', 'third', 'fourth', 'missing']
Related Posts Related Posts :
  • unable to post file+data using python-requests
  • How to test aws lambda functions locally
  • inconsistent plot between matplotlib and seaborn in Python
  • How matplotlib show obvious changes?
  • Project in Python3, reading files, word data
  • Check for specific Item in list without Iteration or find()
  • Unicode encoding when reading from text file
  • Overloaded variables in python for loops?
  • All elements have same value after appending new element
  • Python Threading loop
  • `_pickle.UnpicklingError: the STRING opcode argument must be quoted`
  • Python: How to stop a variable from exceeding a value?
  • python textblob and text classification
  • Django - Context dictionary for attribute inside a class
  • Database is not updated in Celery task with Flask and SQLAlchemy
  • Shapely intersections vs shapely relationships - inexact?
  • How to extract a percentage column from a periodic column and the sum of the column?
  • Zombie ssh process using python subprocess.Popen
  • Python regex to capture a comma-delimited list of items
  • joining string and long in python
  • Value Error in python numpy
  • Check if any character of a string is uppercase Python
  • TensorFlow - why doesn't this sofmax regression learn anything?
  • Python Anaconda Proxy Setup via .condarc file on Windows
  • Creating django objects from emails
  • Get spotify currently playing track
  • Select multiple columns and remove values according to a list
  • Python - How to Subtract a Variable By 1 Every Second?
  • Tkinter unable to alloc 71867 bytes
  • How to add Variable to JSON Python Django
  • CSRF token missing or invalid Django
  • Python: writing to a text file
  • Extracting multiple rows from pandas dataframe and converting to columns
  • Pinging a remote PC with Flask, causing server to block
  • Making a fractal graph using a 2D array?
  • Replacing a word in a list with a value from a dict
  • Savefig as eps yields a non-usable eps
  • Crispy-forms InlineRadios don't show my model state
  • Getting Title of a text
  • Python with numpy: How to delete an element from each row of a 2-D array according to a specific index
  • Sending and Receive data from a web page - Selenium
  • KeyError with Pyro4
  • Python module error
  • Python :: Attribute in superclass not available in inheriting subclass
  • Why does greater than and unequal operators work even though only less than and equal operator has been overloaded
  • Input length mismatch scikit
  • Print String in decreasing length
  • Overloading the [] operator in python class to refer to a numpy.array data member
  • Sympy - altering the range of the y axis for a plot
  • How do I programmatically list a DLL's dependencies in C++ or Python?
  • How do I lock window resizing in a Python matplotlib window?
  • Word boundary RegEx search using PyMongo
  • Iterating over a string by only changing one element in the string
  • classification of data where attribute values are strings
  • Validate user input using regular expressions
  • Synchronizing and Resampling two timeseries with non-uniform millisecond intraday data
  • determing the number of sentences, words and letters in a text file
  • Deploying impure Python packages to AWS
  • Navigating between multiple Tkinter GUI frames
  • Python - Do I need to remove instances from a dictionary?
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co