- Python convert given list into dict with count
- Count items in list and make it a dictionary [duplicate]
- How do i convert list with word count into a dictionary [duplicate]
- Convert list with repeated values to a dictionary [duplicate]
- Convert a list of tuples into a dictionary and adding count information as dictionary values
- Python Data Structures
- Ten different ways to convert a Python list to a dictionary
- 1. Converting a List of Tuples to a Dictionary
- 2. Converting Two Lists of the Same Length to a Dictionary
Python convert given list into dict with count
The following solution also work : Using counter is more effective for large lists, but dict comprehension is better for shorter lists. Solution 1: here’s a 1 liner for this purpose using the Counter function from collections library: Solution 2: A native implementation, would be to go through the array and count the amount of repeated elements but verifying that they do not exist before in the dictionary, if so, then increase the counter, in a super simple way, you have something like this Solution 3: The solution suggested by gil is good.
Count items in list and make it a dictionary [duplicate]
This is exactly what a Counter is for.
>>> from collections import Counter >>> Counter(['tango', 'bravo', 'tango', 'alpha', 'alpha']) Counter()
You can use the Counter object just like a dictionary, because it is a child class of the builtin dict . Excerpt from the docs:
class Counter(__builtin__.dict)
Dict subclass for counting hashable items. Sometimes called a bag or multiset. Elements are stored as dictionary keys and their counts are stored as dictionary values.
As requested, here’s another way:
>>> names = ['tango', 'bravo', 'tango', 'alpha', 'alpha'] >>> d = <> >>> for name in names: . d[name] = d.get(name, 0) + 1 . >>> d
How to convert a list of lists into a dictionary of relative frequencies?, You’ll need to use tuples rather than lists use them as dict keys (keys need to be to counter the variations, then a dict comprehension:
How do i convert list with word count into a dictionary [duplicate]
You can use setdefault method to set a key’s value to 0 if its not present. If the key is already is present, it will return the value.
Initial Variables
s = 'I want to make a dictionary using a string and count the words of that string' a = [1, 2, 3, 2, 3] d = <>
Different ways of building your dictionary
for xx in s: d[xx] = 1 + d.get(xx, 0) print(d)
Also if you don’t want to use the library, then you can do in this way.
for xx in s: #same way we can use a if xx not in d: d[xx] = 1 else: d[xx]+ = 1
Again we can use Counter which is faster.
from collections import Counter s = list(s) print(Counter(s))
How do i convert list with word count into a dictionary, You can use setdefault method to set a key’s value to 0 if its not present. If the key is already is present, it will return the value.
Convert list with repeated values to a dictionary [duplicate]
here’s a 1 liner for this purpose using the Counter function from collections library:
from collections import Counter l = ['dog', 'bird', 'bird', 'cat', 'dog', 'fish', 'cat', 'cat', 'dog', 'cat', 'bird', 'dog'] print(Counter(l))
A native implementation, would be to go through the array and count the amount of repeated elements but verifying that they do not exist before in the dictionary, if so, then increase the counter, in a super simple way, you have something like this
l = ['dog', 'bird', 'bird', 'cat', 'dog', 'fish', 'cat', 'cat', 'dog', 'cat', 'bird', 'dog'] l_dict = <> for i in l: if i in l_dict: l_dict[i] += 1 else: l_dict[i] = 1 print(l_dict)
The solution suggested by gil is good. The following solution also work :
Using counter is more effective for large lists, but dict comprehension is better for shorter lists.
We can verify it with timeit :
from timeit import timeit from collection import Counter # declare your list here def with_comprehension(): return def with_counter(): return Counter(l)
With your example (12 elements), dict comprehension is better :
>>> timeit(with_comprehension) 0.9273126000771299 >>> timeit(with_counter) 1.1477947999956086
But when you have 100 elements, counter become more effective :
>>> timeit(with_comprehension) 3.6719840000150725 >>> timeit(with_counter) 2.85686399997212
Python — Create a dictionary with comprehension, Python supports dict comprehensions, which allow you to express the creation of dictionaries at runtime using a similarly concise syntax. A dictionary
Convert a list of tuples into a dictionary and adding count information as dictionary values
list_input = [("00", 1), ("10", 2), ("00", 3), ("10", 1), ("00", 2), ("11", 1)] res = <> for k, v in list_input: res[k] = res.get(k, 0) + v print(res)
You can use collections.defaultdict :
from collections import defaultdict list_input = [("00", 1), ("10", 2), ("00", 3), ("10", 1), ("00", 2), ("11", 1)] d = defaultdict(int) for k, v in list_input: d[k] += v print(dict(d))
di = <> for t in list_input: key = t[0] value = t[1] # Initialize key such that you don't get key errors if key not in di: diPython list to dict with count = 0 diPython list to dict with count += value
Python | Count number of items in a dictionary value that is a list, Python | Count number of items in a dictionary value that is a list ; Method #2: Using list comprehension ; Method #3: Using dict.items() ; Method
Python Data Structures
Python lists and dictionaries are two data structures in Python used to store data. A Python list is an ordered sequence of objects, whereas dictionaries are unordered. The items in the list can be accessed by an index (based on their position) whereas items in the dictionary can be accessed by keys and not by their position.
Let’s see how to convert a Python list to a dictionary.
Ten different ways to convert a Python list to a dictionary
- Converting a list of tuples to a dictionary
- Converting two lists of the same length to a dictionary
- Converting two lists of different length to a dictionary
- Converting a list of alternative key, value items to a dictionary
- Converting a list of dictionaries to a single dictionary
- Converting a list into a dictionary using enumerate()
- Converting a list into a dictionary using dictionary comprehension
- Converting a list to a dictionary using dict.fromkeys()
- Converting a nested list to a dictionary using dictionary comprehension
- Converting a list to a dictionary using Counter()
1. Converting a List of Tuples to a Dictionary
The dict() constructor builds dictionaries directly from sequences of key-value pairs.
#Converting list of tuples to dictionary by using dict() constructor
color=[('red',1),('blue',2),('green',3)]
d=dict(color)
print (d)#Output:
2. Converting Two Lists of the Same Length to a Dictionary
We can convert two lists of the same length to the dictionary using zip() .
zip() will return an iterator of tuples. We can convert that zip object to a dictionary using the dict() constructor.