Linked Questions
113 questions linked to/from Create a dictionary with comprehension
542
votes
9
answers
621k
views
Python Dictionary Comprehension [duplicate]
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
Iterate over a dictionary by comprehension and get a dictionary [duplicate]
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
How to add another attribute in dictionary inside a one line for loop [duplicate]
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
Python oneliner to initialize a dictionary [duplicate]
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
.
...
0
votes
2
answers
3k
views
assigning values to dictionary keys with list comprehension [duplicate]
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
code is invalid under python 2.6 but fine in 2.7 [duplicate]
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
Python dictionary function resulting in syntax error on Linux but not on Windows [duplicate]
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
how to merge two dict in python? [duplicate]
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 ...
0
votes
2
answers
122
views
Why is this invalid syntax within for loop? [duplicate]
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 ...
1
vote
1
answer
117
views
Create a dictionary from a nested list [duplicate]
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
Python loop statement postposition explaination [duplicate]
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)
...
0
votes
1
answer
47
views
Use list elements to initalise dictionary of empty lists Python [duplicate]
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
How can this for loop code work? (pandas, jupyter, python) [duplicate]
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
Could anyone explain this syntax? [duplicate]
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: '...
-1
votes
2
answers
58
views
How to create dictionary from list [duplicate]
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 = {'...