dictionary-comprehension

TypeError with List Comprehension When Trying to Create a Dictionary from Tuples

TypeError with List Comprehension When Trying to Create a Dictionary from Tuples Question: I’m encountering a TypeError: unhashable type: ‘list’ while attempting to create a dictionary from tuples using list comprehension, and I can’t figure out why this error occurs as I’m not using any lists in my code. Here’s the snippet of code that’s …

Total answers: 1

Create dataframes with names from a list

Create dataframes with names from a list Question: I have excel files with many tabs. I want to concat all of them, one tab at a time. I am doing: mypath = "mypath" files = os.listdir(mypath) files = [os.path.join(mypath,f) for f in files if f[-4:]==’xlsx’] sheets = pandas.ExcelFile(files[0]).sheet_names Now, say my tabs are alpha, beta, …

Total answers: 1

Creating a dictionary from a text archive (Codon table)

Creating a dictionary from a text archive (Codon table) Question: I want to create a dictionary with an archive that I’ve downloaded from Rosalind Code Project. I saved it like table.txt and written this code: d = {} with open(‘./table.txt’,”r”) as f: a = f.read().split() for line in a: if len(line) == 3: k = …

Total answers: 1

Return copy of dictionary excluding specified keys

Return copy of dictionary excluding specified keys Question: I want to make a function that returns a copy of a dictionary excluding keys specified in a list. Considering this dictionary: my_dict = { “keyA”: 1, “keyB”: 2, “keyC”: 3 } A call to without_keys(my_dict, [‘keyB’, ‘keyC’]) should return: { “keyA”: 1 } I would like …

Total answers: 7

Python Dict Comprehension to Create and Update Dictionary

Python Dict Comprehension to Create and Update Dictionary Question: I have a list of dictionaries (data) and want to convert it into dictionary (x) as below. I am using following ‘for loop’ to achieve. data = [{‘Dept’: ‘0123’, ‘Name’: ‘Tom’}, {‘Dept’: ‘0123’, ‘Name’: ‘Cheryl’}, {‘Dept’: ‘0123’, ‘Name’: ‘Raj’}, {‘Dept’: ‘0999’, ‘Name’: ‘Tina’}] x = {} …

Total answers: 2

Dictionary comprehension with lambda expression fails to produce desired result

Dictionary comprehension with lambda expression fails to produce desired result Question: I’m creating a one liner to map the string of int to a function testing if the values are matched. Ideally, the result dictionary d behaves like d[‘0’](0) is True and d[‘0’](1) is False. But instead, I get the following output: >>> d = …

Total answers: 2

multiple key value pairs in dict comprehension

multiple key value pairs in dict comprehension Question: I am trying to create multiple key : value pairs in a dict comprehension like this: {‘ID’: (e[0]), ‘post_author’: (e[1]) for e in wp_users} I am receiving “missing ‘,'” I have also tried it this way: [{‘ID’: (e[0]), ‘post_author’: (e[1])} for e in wp_users] I then receive …

Total answers: 3

OrderedDict comprehensions

OrderedDict comprehensions Question: Can I extend syntax in python for dict comprehensions for other dicts, like the OrderedDict in collections module or my own types which inherit from dict? Just rebinding the dict name obviously doesn’t work, the {key: value} comprehension syntax still gives you a plain old dict for comprehensions and literals. >>> from …

Total answers: 3

Alternative to dict comprehension prior to Python 2.7

Alternative to dict comprehension prior to Python 2.7 Question: How can I make the following functionality compatible with versions of Python earlier than Python 2.7? gwfuncs = [reboot, flush_macs, flush_cache, new_gw, revert_gw, send_log] gw_func_dict = {chr(2**i): func for i, func in enumerate(gwfuncs[:8])} Asked By: stdcerr || Source Answers: Use: gw_func_dict = dict((chr(2**i), func) for i, …

Total answers: 1

Build Dictionary in Python Loop – List and Dictionary Comprehensions

Build Dictionary in Python Loop – List and Dictionary Comprehensions Question: I’m playing with some loops in python. I am quite familiar with using the “for” loop: for x in y: do something You can also create a simple list using a loop: i = [] for x in y: i.append(x) and then I recently …

Total answers: 3