All Questions
Tagged with list dictionary
12,489
questions
2780
votes
22
answers
1.4m
views
How to sort a list of dictionaries by a value of the dictionary in Python?
How do I sort a list of dictionaries by a specific key's value? Given:
[{'name': 'Homer', 'age': 39}, {'name': 'Bart', 'age': 10}]
When sorted by name, it should become:
[{'name': 'Bart', 'age': 10}, ...
1744
votes
22
answers
1.3m
views
Make a dictionary (dict) from separate lists of keys and values
I want to combine these:
keys = ['name', 'age', 'food']
values = ['Monty', 42, 'spam']
into a single dictionary:
{'name': 'Monty', 'age': 42, 'food': 'spam'}
How can I do this?
1298
votes
13
answers
2.0m
views
How do I return dictionary keys as a list in Python?
With Python 2.7, I can get dictionary keys, values, or items as a list:
>>> newdict = {1:0, 2:0, 3:0}
>>> newdict.keys()
[1, 2, 3]
With Python >= 3.3, I get:
>>> newdict....
811
votes
13
answers
1.1m
views
How do I convert a Map to List in Java?
How do I convert a Map<key,value> to a List<value>? Should I iterate over all map values and insert them into a list?
641
votes
7
answers
1.6m
views
How can I get list of values from dict?
How can I get a list of the values in a dict in Python?
In Java, getting the values of a Map as a List is as easy as doing list = map.values();. I'm wondering if there is a similarly simple way in ...
511
votes
15
answers
555k
views
How can I avoid "RuntimeError: dictionary changed size during iteration" error?
Suppose I have a dictionary of lists:
d = {'a': [1], 'b': [1, 2], 'c': [], 'd':[]}
Now I want to remove key-value pairs where the values are empty lists. I tried this code:
for i in d:
if not d[i]...
407
votes
6
answers
307k
views
Pandas DataFrame to List of Dictionaries
I have the following DataFrame:
customer item1 item2 item3
1 apple milk tomato
2 water orange potato
3 juice mango chips
which I want ...
339
votes
13
answers
229k
views
In Python, when to use a Dictionary, List or Set?
When should I use a dictionary, list or set?
Are there scenarios that are more suited for each data type?
317
votes
10
answers
436k
views
Getting a list of values from a list of dicts
I have a list of dicts like this:
[{'value': 'apple', 'blah': 2},
{'value': 'banana', 'blah': 3} ,
{'value': 'cars', 'blah': 4}]
I want ['apple', 'banana', 'cars']
Whats the best way to do this?...
299
votes
7
answers
1.2m
views
Converting Dictionary to List? [duplicate]
I'm trying to convert a Python dictionary into a Python list, in order to perform some calculations.
#My dictionary
dict = {}
dict['Capital']="London"
dict['Food']="Fish&Chips"
dict['2012']="...
276
votes
19
answers
243k
views
Remove duplicate dict in list in Python
I have a list of dicts, and I'd like to remove the dicts with identical key and value pairs.
For this list: [{'a': 123}, {'b': 123}, {'a': 123}]
I'd like to return this: [{'a': 123}, {'b': 123}]
...
267
votes
15
answers
680k
views
How to extract all values from a dictionary in Python?
I have a dictionary d = {1:-0.3246, 2:-0.9185, 3:-3985, ...}.
How do I extract all of the values of d into a list l?
263
votes
13
answers
236k
views
How can I convert a dictionary into a list of tuples?
If I have a dictionary like:
{'a': 1, 'b': 2, 'c': 3}
How can I convert it to this?
[('a', 1), ('b', 2), ('c', 3)]
And how can I convert it to this?
[(1, 'a'), (2, 'b'), (3, 'c')]
248
votes
21
answers
715k
views
HashMap with multiple values under the same key
Is it possible to implement a HashMap with one key and two values?
Just as HashMap<userId, clientID,timeStamp>?
If not, is there any other way to implement the storage of multiple values e.g. ...
240
votes
12
answers
221k
views
How do I merge a list of dicts into a single dict?
How can I turn a list of dicts like [{'a':1}, {'b':2}, {'c':1}, {'d':2}], into a single dict like {'a':1, 'b':2, 'c':1, 'd':2}?
Answers here will overwrite keys that match between two of the input ...
233
votes
11
answers
245k
views
Create a list with initial capacity in Python
Code like this often happens:
l = []
while foo:
# baz
l.append(bar)
# qux
This is really slow if you're about to append thousands of elements to your list, as the list will have to be ...
231
votes
12
answers
584k
views
Convert [key1,val1,key2,val2] to a dict?
Let's say I have a list a in Python whose entries conveniently map to a dictionary. Each even element represents the key to the dictionary, and the following odd element is the value
for example,
a =...
224
votes
8
answers
413k
views
How do I get the list of keys in a Dictionary?
I only want the Keys and not the Values of a Dictionary.
I haven't been able to get any code to do this yet. Using another array proved to be too much work as I use remove also.
How do I get a List of ...
202
votes
8
answers
228k
views
Check if value already exists within list of dictionaries in Python?
I've got a Python list of dictionaries as follows:
a = [
{'main_color': 'red', 'second_color':'blue'},
{'main_color': 'yellow', 'second_color':'green'},
{'main_color': 'yellow', '...
182
votes
5
answers
315k
views
C# Convert List<string> to Dictionary<string, string>
This may seem an odd thing to want to do but ignoring that, is there a nice concise way of converting a List<string> to Dictionary<string, string> where each Key Value Pair in the ...
174
votes
7
answers
961k
views
How to overcome TypeError: unhashable type: 'list' [duplicate]
I'm trying to take a file that looks like this:
AAA x 111
AAB x 111
AAA x 112
AAC x 123
...
And use a dictionary so that the output looks like this
{AAA: ['111', '112'], AAB: ['111'], AAC: [123], ...}...
170
votes
5
answers
161k
views
List of tuples to dictionary [duplicate]
Here's how I'm currently converting a list of tuples to dictionary in Python:
l = [('a',1),('b',2)]
h = {}
[h.update({k:v}) for k,v in l]
> [None, None]
h
> {'a': 1, 'b': 2}
Is there a better ...
162
votes
11
answers
152k
views
What's the difference between [] and {} vs list() and dict()? [closed]
I understand that they are both essentially the same thing. But in terms of creating an empty list or dict, are there any differences?
153
votes
3
answers
402k
views
Appending to list in Python dictionary [duplicate]
Is there a more elegant way to write this code?
What I am doing: I have keys and dates. There can be a number of dates assigned to a key and so I am creating a dictionary of lists of dates to ...
152
votes
6
answers
130k
views
How to find the min/max value of a common key in a list of dicts?
I have a list of dictionaries like so:
[{'price': 99, 'barcode': '2342355'}, {'price': 88, 'barcode': '2345566'}]
I want to find the min() and max() prices. Now, I can sort this easily enough using a ...
142
votes
7
answers
144k
views
How to convert list of key-value tuples into dictionary?
I have a list that looks like:
[('A', 1), ('B', 2), ('C', 3)]
I want to turn it into a dictionary that looks like:
{'A': 1, 'B': 2, 'C': 3}
What's the best way to go about this?
EDIT: My list of ...
142
votes
5
answers
263k
views
Iterating Over Dictionary Key Values Corresponding to List in Python
Working in Python 2.7. I have a dictionary with team names as the keys and the amount of runs scored and allowed for each team as the value list:
NL_East = {'Phillies': [645, 469], 'Braves': [599, ...
140
votes
11
answers
172k
views
Why can't I use a list as a dict key in python? Exactly what can and cannot be used, and why?
I found that the following are all valid:
>>> d = {}
>>> d[None] = 'foo'
>>> d[(1, 3)] = 'baz'
Even a module can be used as a dict key:
>>> import sys
>>> ...
135
votes
14
answers
66k
views
List of dicts to/from dict of lists
I want to change back and forth between a dictionary of (equal-length) lists:
DL = {'a': [0, 1], 'b': [2, 3]}
and a list of dictionaries:
LD = [{'a': 0, 'b': 2}, {'a': 1, 'b': 3}]
134
votes
4
answers
244k
views
python filter list of dictionaries based on key value
I have a list of dictionaries and each dictionary has a key of (let's say) 'type' which can have values of 'type1', 'type2', etc. My goal is to filter out these dictionaries into a list of the same ...
131
votes
7
answers
309k
views
Convert dictionary to list collection in C#
I have a problem when trying to convert a dictionary to list.
Example if I have a dictionary with template string as key and string as value. Then I wish to convert the dictionary key to list ...
131
votes
3
answers
221k
views
Python: converting a list of dictionaries to json
I have a list of dictionaries, looking some thing like this:
list = [{'id': 123, 'data': 'qwerty', 'indices': [1,10]}, {'id': 345, 'data': 'mnbvc', 'indices': [2,11]}]
and so on. There may be more ...
129
votes
6
answers
175k
views
How do I initialize a dictionary of empty lists in Python?
My attempt to programmatically create a dictionary of lists is failing to allow me to individually address dictionary keys. Whenever I create the dictionary of lists and try to append to one key, all ...
122
votes
6
answers
172k
views
How do I combine two lists into a dictionary in Python? [duplicate]
I have two lists of the same length:
[1,2,3,4] and [a,b,c,d]
I want to create a dictionary where I have {1:a, 2:b, 3:c, 4:d}
What's the best way to do this?
110
votes
9
answers
102k
views
How to convert a List into a Map in Dart
I looking for an on-the-shelf way to convert a List into a Map in Dart.
In python for example you can do:
l= [ ('a',(1,2)), ('b',(2,3)), ('c',(3,4) ) ]
d=dict(l)
==> {'a': (1, 2), 'c': (3, 4), 'b'...
108
votes
13
answers
117k
views
In what case would I use a tuple as a dictionary key? [closed]
I was studying the difference between lists and tuples (in Python). An obvious one is that tuples are immutable (the values cannot be changed after initial assignment), while lists are mutable.
A ...
108
votes
3
answers
251k
views
Appending a dictionary to a list - I see a pointer like behavior
I tried the following in the python interpreter:
>>> a = []
>>> b = {1:'one'}
>>> a.append(b)
>>> a
[{1: 'one'}]
>>> b[1] = 'ONE'
>>> a
[{1: 'ONE'}...
108
votes
2
answers
395k
views
Python, TypeError: unhashable type: 'list'
I'm receiving the following error in my program. The traceback:
Traceback (most recent call last):
File "C:\Python33\Archive\PythonGrafos\Alpha.py", line 126, in <module>
menugrafos()
...
105
votes
5
answers
134k
views
One liner: creating a dictionary from list with indices as keys
I want to create a dictionary out of a given list, in just one line. The keys of the dictionary will be indices, and values will be the elements of the list. Something like this:
a = [51,27,13,56] ...
105
votes
4
answers
477k
views
Convert list into a dictionary [duplicate]
l = ["a", "b", "c", "d", "e"]
I want to convert this list to a dictionary like:
d = {"a": "b", "c": "d", "e": ""}
So basically, the evens will be keys whereas the odds will be values. I know that I ...
103
votes
5
answers
132k
views
Python - sum values in dictionary
I have got pretty simple list:
example_list = [
{'points': 400, 'gold': 2480},
{'points': 100, 'gold': 610},
{'points': 100, 'gold': 620},
{'points': 100, 'gold': 620}
]
How can I ...
99
votes
8
answers
102k
views
Python: get a dict from a list based on something inside the dict
I need to be able to find an item in a list (an item in this case being a dict) based on some value inside that dict. The structure of the list I need to process looks like this:
[
{
'...
94
votes
10
answers
193k
views
How to get dictionary values as a generic list, when each entry's value is a list
I just want get a list from Dictionary values but it's not so simple as it appears !
here the code :
Dictionary<string, List<MyType>> myDico = GetDictionary();
List<MyType> items = ...
90
votes
10
answers
118k
views
Remove dictionary from list
If I have a list of dictionaries, say:
[{'id': 1, 'name': 'paul'},
{'id': 2, 'name': 'john'}]
and I would like to remove the dictionary with id of 2 (or name 'john'), what is the most efficient way ...
89
votes
9
answers
94k
views
Mapping dictionary value to list
Given the following dictionary:
dct = {'a':3, 'b':3,'c':5,'d':3}
How can I apply these values to a list such as:
lst = ['c', 'd', 'a', 'b', 'd']
in order to get something like:
lstval = [5, 3, 3, ...
86
votes
3
answers
225k
views
Python Array with String Indices
Is it possible to use strings as indices in an array in python?
For example:
myArray = []
myArray["john"] = "johns value"
myArray["jeff"] = "jeffs value"
print myArray["john"]
86
votes
9
answers
160k
views
How do I serialize a Python dictionary into a string, and then back to a dictionary?
How do I serialize a Python dictionary into a string, and then back to a dictionary? The dictionary will have lists and other dictionaries inside it.
82
votes
11
answers
167k
views
how to convert list of dict to dict
How to convert list of dict to dict. Below is the list of dict
data = [{'name': 'John Doe', 'age': 37, 'sex': 'M'},
{'name': 'Lisa Simpson', 'age': 17, 'sex': 'F'},
{'name': 'Bill ...
79
votes
1
answer
21k
views
Is there a class like Dictionary<> in C#, but for just keys, no values?
I guess another way to phrase this would be "Is there a class like List<> in C#, but optimized for checking whether a particular value is present?" I'm sure for a small set of values List<>...
76
votes
8
answers
240k
views
How can I create an array/list of dictionaries in python?
I have a dictionary as follows:
{'A':0,'C':0,'G':0,'T':0}
I want to create an array with many dictionaries in it, as follows:
[{'A':0,'C':0,'G':0,'T':0},{'A':0,'C':0,'G':0,'T':0},{'A':0,'C':0,'G':0,...