A one liner, just for fun:
all_examples = ['A,1,1', 'B,2,1', 'C,4,4', 'D,4,5']
map(dict, zip(*[[(s[0], int(x)) for x in s.split(',')[1:]] for s in all_examples]))
Produces:
[{'A': 1, 'C': 4, 'B': 2, 'D': 4},
{'A': 1, 'C': 4, 'B': 1, 'D': 5}]
As a bonus, this will work for longer sequences too:
all_examples = ['A,1,1,1', 'B,2,1,2', 'C,4,4,3', 'D,4,5,6']
Output:
[{'A': 1, 'C': 4, 'B': 2, 'D': 4},
{'A': 1, 'C': 4, 'B': 1, 'D': 5},
{'A': 1, 'C': 3, 'B': 2, 'D': 6}]
Explanation:
map(dict, zip(*[[(s[0], int(x)) for x in s.split(',')[1:]] for s in all_examples]))
[... for s in all_examples]For each element in your list:s.split(',')[1:]Split it by commas, then take each element after the first(...) for x inand turn it into a list of tupless[0], int(x)of the first letter, with that element converted to integerzip(*[...])now transpose your lists of tuplesmap(dict, ...)and turn each one into a dictionary!
A one liner, just for fun:
all_examples = ['A,1,1', 'B,2,1', 'C,4,4', 'D,4,5']
map(dict, zip(*[[(s[0], int(x)) for x in s.split(',')[1:]] for s in all_examples]))
Produces:
[{'A': 1, 'C': 4, 'B': 2, 'D': 4},
{'A': 1, 'C': 4, 'B': 1, 'D': 5}]
As a bonus, this will work for longer sequences too:
all_examples = ['A,1,1,1', 'B,2,1,2', 'C,4,4,3', 'D,4,5,6']
Output:
[{'A': 1, 'C': 4, 'B': 2, 'D': 4},
{'A': 1, 'C': 4, 'B': 1, 'D': 5},
{'A': 1, 'C': 3, 'B': 2, 'D': 6}]
Explanation:
map(dict, zip(*[[(s[0], int(x)) for x in s.split(',')[1:]] for s in all_examples]))
[... for s in all_examples]For each element in your list:s.split(',')[1:]Split it by commas, then take each element after the first(...) for x inand turn it into a list of tupless[0], int(x)of the first letter, with that element converted to integerzip(*[...])now transpose your lists of tuplesmap(dict, ...)and turn each one into a dictionary!
Also just for fun, but with a focus on understandability:
all_examples = ['A,1,1', 'B,2,1', 'C,4,4', 'D,4,5']
ll = [ x.split(",") for x in all_examples ]
ld = list()
for col in range(1, len(ll[0])):
ld.append({ l[0] : int(l[col]) for l in ll })
print ld
will print
[{'A': 1, 'C': 4, 'B': 2, 'D': 4}, {'A': 1, 'C': 4, 'B': 1, 'D': 5}]
Works as long as the input is csv with integers and lines are same length.
Dissection: I will use the teminology "thing" for A, B and C and "measurement" for the "columns" in the data, i.e. those values in the same "csv-column" of the inut data.
Get the string input data into a list for each line: A,1,1 -> ["A","1","1"]
ll = [ x.split(",") for x in all_examples ]
The result is supposed to be a list of dicts, so let's initialize one:
ld = list()
For each measurement (assuming that all lines have the same number of columns):
for col in range(1, len(ll[0])):
Take the thing l[0], e.g. "A", from the line and assign the numeric value int(), e.g. 1, of the measurement in the respective column l[col], e.g. "1", to the thing. Then use a dictionary comprehension to combine it into the next line of the desired result. Finally append() the dict to the result list ld.
ld.append({ l[0] : int(l[col]) for l in ll })
View unfoamtted. Use print json.dumps(ld, indent=4) for more convenient display:
print ld
Hope this helps. Find more on dict comprehensions e.g. here (Python3 version of this great book).
How to properly copy a list of dictionaries in python?
Seeking to populate a dictionary from a series of lists
how to know when to use Lists and when to Use dictionaries?
This is a complex question because there are times when either a dictionary, list, or tuple will work; and there are times where only one or two would work.
Dict
A dict does not* maintain order. Think of it like a literal dictionary: if I want to know the value of something, I just have to know its key. It's location inside the dict is completely irrelevant as long as I know the key. This makes it great for retrieving things that you can give a logical name to and updating things that you can give a logical name to. It's extremely fast for looking up the value of something. similar to a real dictionary, you have a good idea of where to start "searching" in an actual dictionary to find a word and don't have to flip through each and every page of the dictionary to find what you're looking for.
List
A list maintains order. If I load values into a list, they're always guaranteed to be in that order forever unless I explicitly change it. This isn't the case with dicts*. This allows you to do things like sort and order data in meaningful ways. A draw back with a list is that finding out if a value exists in a list could take a very long time if the items are unordered.
If I have an unordered list like this:
[['Hannah', 'black'], ['Mark', 'brown'], ['Avery', 'blonde'],....]
And I want to lookup the hair color of 'Peter', I have to search through every single value in the list to see if it matches Peter. If 'Peter' doesn't exist in the list, I will have gone through every value in the list. If the list of thousands of names long, this will take thousands of operations. However, if I had a dict mapping name to hair color, looking up if Peter exists in the dict would take roughly 1 operation, even if the dict was millions of mappings long.
Tuple
Tuples a bit like immutable lists. Meaning they hold values, and maintain order, but once you create one, you can never modify/add/or delete any of its values. A good use case of a tuple are related data that comes in fixed sizes and the order has a specific meaning:
# rgb color values rgb = (255, 120, 30) # x, y coordinates coordinate = (-1, 0)
Tuples are also great for what I think of as "throw-away-lists". A list that you only use once, that will never grow, shrink, or change values can probably be turned into a tuple.
for name in ['Bob', 'Peter', 'Sarah']:
print(name)
for name in ('Bob', 'Peter', 'Sarah'):
print(name)In the example above, both have the exact same output, although you will see people use either one. The list of names is never growing, shrinking, or changing value, so it can be converted to a tuple and probably should be converted to a tuple because a tuple is more efficient than a list.
-------------------------------------------------------------
In your example both dicts and lists would work, so why did the Automate The Boarding Stuff book use a dict for the Tic Tac Toe board instead of a list? (1) Probably because dict is a little harder to grasp so it would be good practice for the reader to get more exposure to using a dict. (2) If we did the list implementation, you would have to put the board in some type of order. Like this:
['', 'X', 'O', '', 'X', '', 'X', 'O', 'O']
One problem with the list implementation is that it is not obvious which index corresponds to which square on a Tic Tac Toe board. Are the first 3 indexes representing the top row? Or do they represent the left column? There's no way to really telling from the code alone. With the dict implementation, it's extremely obvious which value corresponds to which square on a Tic Tac Toe board.
But in this case, yes either a list or dict would work. A tuple would probably not be a great idea because it's immutable, and you can't update the values it holds without creating a brand new tuple.
-------------------------------------------------------------
* later version of Python does maintain dict entry order
More on reddit.comWhen to use array vs. list (vs dict)
Videos
I write web scrapers and often work with a huge list of dictionaries.
All of the dictionaries are identical or near identical in terms of their keys.
Each dictionary represents content scraped from a webpage.
Often time I find myself looping through the list of dictionaries to perform operations but I feel like there must be a better way. Is there anyway I can treat them all as one dictionary and apply functions to a particular key?
Any advice would be appreciated tia