Two Lists Into One Dictionary Python

Mark Corrigan

If I have two lists

dates = []
closes = []

And I have one dictionary

dict2write = {'date', 'close'}

How do I fill the dictionary with the two lists? I'm going to write the dictionary into a csv.

mhawke

To make a dictionary use zip() to form tuples and then dict() to create the dictionary.

>>> dates = ['2014-07-31', '2013-11-22', '2014-01-01']
>>> closes = ['what', 'is', 'this?']
>>> zip(dates, closes)
[('2014-07-31', 'what'), ('2013-11-22', 'is'), ('2014-01-01', 'this?')]
>>> d = dict(zip(dates, closes))
>>> d
{'2013-11-22': 'is', '2014-07-31': 'what', '2014-01-01': 'this?'}

But if you want to write the data to a CSV file, you don't need to create dictionaries, you just need to zip the lists.

import csv
with open('data.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerows(zip(dates, closes))

Output in data.csv is:

2014-07-31,what
2013-11-22,is
2014-01-01,this?

One other thing worth mentioning, if the lengths of the lists vary you can use itertools.izip_longest() to zip the lists. e.g.

import csv
from itertools import izip_longest
dates = ['2014-07-31', '2013-11-22', '2014-01-01']
closes = ["what's", 'this?']
with open('data.csv', 'w') as f:
    csv.writer(f).writerows(izip_longest(dates, closes))

This will leave the missing columns empty in the resultant CSV file:

2014-07-31,what's
2013-11-22,this?
2014-01-01,

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Adding Two Lists into One Dictionary (Python)

Join two lists into one dictionary

Python dictionary from two lists

Python: Merge two lists to dictionary

Map two lists (one of which is a list of lists) into a dictionary

Map two lists (one of which is a list of lists) into a dictionary

How to join two lists, if one is a tuple with multiple lists, into a dictionary…

Two-keys dictionary into one key dictionary of lists

Merge two lists in python to one

Python: merge two lists of dictionaries where one dictionary has a unique column

Python 2.7.12: How to combine two lists into a dictionary

How to add two category lists to dictionary in python

How to concatenate two dictionary lists together in python?

How to zip 3 lists into one dictionary in python?

CSV to Python Dictionary with multiple lists for one key

A better way to create a dictionary out of two lists with duplicated values in one

split dictionary into two lists , one list is for key, another list is for value

Adding two values to one key in lists and add the list to dictionary

Creating nested dictionary from two lists one of which contains dictionaries

python: I want to make a dictionary using two, two dimensional lists

Dictionary containing two lists

Convert two lists into a dictionary

merge two lists into dictionary

Two nested lists into dictionary

Python - sort one list from two lists

How to merge two lists into one list in python

Python - Combine two lists into one json object

Python assigning two lists in one line

Create two lists from one list in python?