Linked Questions

113 questions linked to/from Create a dictionary with comprehension
542 votes
9 answers
621k views

Is it possible to create a dictionary comprehension in Python (for the keys)? Without list comprehensions, you can use something like this: l = [] for n in range(1, 11): l.append(n) We can ...
21 votes
4 answers
35k views

How to iterate over a dictionary by dictionary comprehension to process it. >>> mime_types={ '.xbm': 'image/x-xbitmap', '.dwg': 'image/vnd.dwg', '.fst': 'image/vnd.fst', '....
9 votes
5 answers
4k views

I have a list of dictionary and a string. I want to add a selected attribute in each dictionary inside the list. I am wondering if this is possible using a one liner. Here are my inputs: saved_fields =...
4 votes
4 answers
11k views

I am looking for a short and compact one-liner to initialize a dictionary from a list in Python. Is there an equivalent to the two constructs below? dic = {} for elt in list: dic[elt.hash] = elt . ...
PPC's user avatar
  • 2,003
0 votes
2 answers
3k views

This is mostly a question of good/pythonic style. I have a dictionary which has lists for values, i.e. my_dict = {"a": a_list, "b": b_list, "c": c_list} and so on. I also have an empty dictionary ...
-1 votes
2 answers
1k views

can someone help me translate some python 2.7 syntax to python 2.6 please (kinda stuck on 2.6 due to redhat dependencies) so i have a simple function to construct a tree: def tree(): return ...
1 vote
1 answer
1k views

I have a line of code that creates a dictionary by setting its key as that of the key of dictionary_b. It then uses the value of that key in dictionary_b as the key to look through dictionary_a to ...
-4 votes
2 answers
304 views

I have a dict struct such like this: a = {'1' : {}, '2' : {}} b = {'3' : 3, '4' : 4} I want to have the following format: a = {'1' : { '3' : 3 }, '2' : { '4' : 4 } } I have tried many times, but I ...
fx0123's user avatar
  • 113
0 votes
2 answers
122 views

I was trying with the below code and got the error: def preprocess(s): return (word: True for word in s.lower().split()) s1 = 'This is a book' text = preprocess(s1) And then here comes the error ...
Leo Li's user avatar
  • 81
1 vote
1 answer
117 views

I am looking to convert the a list of 2-element lists to a dictionary. Note that I do not want to use group_by that has different outcomes than a simple conversion to dict. Is this possible? The two ...
0 votes
0 answers
87 views

Come from the Ruby world, Python for me is a little "stange". Someone's code: the_dict = {"a": 1, "b":2} another_dict = { key: val for key, val in the_dict.iteritems() } print(another_dict) ...
chailong's user avatar
  • 346
0 votes
1 answer
47 views

I have a list. I want to use its elements to initialise a dictionary, which n pairs of key:list(). Current input : names = ['John', 'Jane', 'Mark'] desired output after transformation : names_dict = {'...
0 votes
1 answer
52 views

i am a student working in bioinformatics using scanpy, python. During finding marker genes, i was going to make a data frame. The following is the code. pd.DataFrame({group + '_' + key[:1] : result[...
0 votes
0 answers
50 views

For context, here is the entire snippet of code. sample_dict = {1: 'r099', 2: 'g444', 3: 't555', 4: 'f444', 5: 'h666'} print(sample_dict) >>> {1: 'r099', 2: 'g444', 3: 't555', 4: 'f444', 5: '...
hadens's user avatar
  • 1
-1 votes
2 answers
58 views

Im new to python so I apologize in advance if this is a bit rudimentary, but given the list: lst = ['user1', 25, 'user2', 10, 'user3', 54] how would I create a dictionary that would be: usr_dict = {'...
Sapien's user avatar
  • 1

15 30 50 per page
1
2 3 4 5
...
8