import csv
with open("in.csv") as csvfile:
    reader = csv.DictReader(csvfile,delimiter=" ")
    print(list(reader))
[{'first_name': 'Baked', 'last_name': 'Beans'}, {'first_name': 'Lovely', 'last_name': 'Spam'}, {'first_name': 'Wonderful', 'last_name': 'Spam'}]

If the delimiter is not actually a , you need to specify " " or whatever it is.

Just to clear any confusion, the code works fine for python3.6 also, the only difference is that using DictReader gives Orderdicts by default:

In [1]: import csv
   ...: with open("in.csv") as csvfile:
   ...:     reader = csv.DictReader(csvfile, delimiter=" ")
   ...:     print(list(reader))
   ...:     
[OrderedDict([('first_name', 'Baked'), ('last_name', 'Beans')]), OrderedDict([('first_name', 'Lovely'), ('last_name', 'Spam')]), OrderedDict([('first_name', 'Wonderful'), ('last_name', 'Spam')])]

You can access keys exactly the same, an OrderedDict just keeps key insertion order:

In [2]: import csv
   ...: with open("in.csv") as csvfile:
   ...:     reader = csv.DictReader(csvfile, delimiter=" ")
   ...:     for dct in reader:
   ...:         print(f"{dct['first_name']} {dct['last_name']}")
   ...:         
   ...:     
Baked Beans
Lovely Spam
Wonderful Spam

Which py3.6 actually does too, so if for some reason you really want a dict:

In [5]: import csv
   ...: with open("in.csv") as csvfile:
   ...:     reader = csv.DictReader(csvfile, delimiter=" ")
   ...:     for dct in map(dict, reader):
   ...:         print(dct)
   ...:         print(f"{dct['first_name']} {dct['last_name']}")
   ...:         
   ...:     
{'first_name': 'Baked', 'last_name': 'Beans'}
Baked Beans
{'first_name': 'Lovely', 'last_name': 'Spam'}
Lovely Spam
{'first_name': 'Wonderful', 'last_name': 'Spam'}
Wonderful Spam

The ordering retention on insertion in py3.6 is an implementation detail and may change, but if enough of us use it, it may just have to stay :)

Answer from Padraic Cunningham on Stack Overflow
🌐
/overlaid
overlaid.net › home › convert a csv to a dictionary in python
Convert a CSV to a Dictionary in Python - /overlaid
February 4, 2016 - I am also new to python but understand the concepts and programming style. ... # Function to convert a csv file to a list of dictionaries. Takes in one variable called “variables_file” ... # Open variable-based csv, iterate over the rows and map values to a list of dictionaries containing key/value pairs · reader = csv.DictReader(open(‘C:/Users/tug02471/Documents/Backup-Aug16/MyPython/Python/dataset’,’rb’)) dict_list = [] for line in reader: dict_list.append(line) return dict_list
Top answer
1 of 5
54
import csv
with open("in.csv") as csvfile:
    reader = csv.DictReader(csvfile,delimiter=" ")
    print(list(reader))
[{'first_name': 'Baked', 'last_name': 'Beans'}, {'first_name': 'Lovely', 'last_name': 'Spam'}, {'first_name': 'Wonderful', 'last_name': 'Spam'}]

If the delimiter is not actually a , you need to specify " " or whatever it is.

Just to clear any confusion, the code works fine for python3.6 also, the only difference is that using DictReader gives Orderdicts by default:

In [1]: import csv
   ...: with open("in.csv") as csvfile:
   ...:     reader = csv.DictReader(csvfile, delimiter=" ")
   ...:     print(list(reader))
   ...:     
[OrderedDict([('first_name', 'Baked'), ('last_name', 'Beans')]), OrderedDict([('first_name', 'Lovely'), ('last_name', 'Spam')]), OrderedDict([('first_name', 'Wonderful'), ('last_name', 'Spam')])]

You can access keys exactly the same, an OrderedDict just keeps key insertion order:

In [2]: import csv
   ...: with open("in.csv") as csvfile:
   ...:     reader = csv.DictReader(csvfile, delimiter=" ")
   ...:     for dct in reader:
   ...:         print(f"{dct['first_name']} {dct['last_name']}")
   ...:         
   ...:     
Baked Beans
Lovely Spam
Wonderful Spam

Which py3.6 actually does too, so if for some reason you really want a dict:

In [5]: import csv
   ...: with open("in.csv") as csvfile:
   ...:     reader = csv.DictReader(csvfile, delimiter=" ")
   ...:     for dct in map(dict, reader):
   ...:         print(dct)
   ...:         print(f"{dct['first_name']} {dct['last_name']}")
   ...:         
   ...:     
{'first_name': 'Baked', 'last_name': 'Beans'}
Baked Beans
{'first_name': 'Lovely', 'last_name': 'Spam'}
Lovely Spam
{'first_name': 'Wonderful', 'last_name': 'Spam'}
Wonderful Spam

The ordering retention on insertion in py3.6 is an implementation detail and may change, but if enough of us use it, it may just have to stay :)

2 of 5
26

Use list():

print(list(reader))

Demo:

>>> with open('names.csv') as csvfile:
...     reader = csv.DictReader(csvfile, delimiter=" ")
...     print(list(reader))
... 
[{'first_name': 'Baked', 'last_name': 'Beans'}, {'first_name': 'Lovely', 'last_name': 'Spam'}, {'first_name': 'Wonderful', 'last_name': 'Spam'}]
Discussions

Loading csv.DictReader() into an actual dict
This is what your code is doing. Say we have an example csv like this: A B 1 2 3 4 reader = csv.DictReader(file) This will open the file. for row in reader: Will read each row in order, and store the results in a dictionary in the format {"A":1, "B":2} dict.update(row) Will update dict, with the values in row. So after the first line dict will be {"A":1, "B":2}. Then you read the next row and update the dict with the new information, which overwrites A and B with the new data so ou will be left with dict = {"A":3, "B":4} Are you sure you dont want a list instead of a dict? Or rather, lists as the values of your dict? More on reddit.com
🌐 r/learnprogramming
8
1
July 22, 2016
What does .dictreader() do?
hi guys! I have a question. I know when we use .dictreader() we use csv module to make a dictionary out of the csv file, so it’s easier to use for programming, but I don’t get it how does this dictionary look like exactly. first, I thought it uses titles in the first line to make keys, ... More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
0
September 27, 2023
python - Creating a dictionary from a csv file? - Stack Overflow
You may iterate over the rows of the csv file dict reader object by iterating over input_file. ... This makes DictReader object not a dictionary(and yes not a key value pair) 2018-11-10T17:52:50.463Z+00:00 ... @HN Singh - Yeah, I know - intention was it will help some one else as well 2018-11-14T06:34:26.543Z+00:00 ... @Palak - it was answered for Python ... More on stackoverflow.com
🌐 stackoverflow.com
python - Best way to convert csv data to dictionaries - Stack Overflow
I have a csv file with following data: val1,val2,val3 1,2,3 22,23,33 How can I convert this data into one dictionary per row, like this? dict1 = {'val1': 1, 'val2': 2, 'val3': 3} dict2 = {'val1': ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Python
docs.python.org › 3 › library › csv.html
csv — CSV File Reading and Writing
When True, raise exception Error on bad CSV input. The default is False. Reader objects (DictReader instances and objects returned by the reader() function) have the following public methods: ... Return the next row of the reader’s iterable object as a list (if the object was returned from reader()) or a dict (if it is a DictReader instance), parsed according to the current Dialect.
🌐
University of Washington
courses.cs.washington.edu › courses › cse140 › 13wi › csv-parsing.html
How to parse csv formatted files using csv.DictReader?
Open the file by calling open and then csv.DictReader. ... You may iterate over the rows of the csv file by iterating ove input_file. (Similarly to other files, you need to re-open the file if you want to iterate a second time.) ... When you iterate over a normal file, each iteration of the ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › load-csv-data-into-list-and-dictionary-using-python
Load CSV data into List and Dictionary using Python - GeeksforGeeks
July 12, 2025 - # importing module import csv # csv fileused id Geeks.csv filename="Geeks.csv" # opening the file using "with" # statement with open(filename,'r') as data: for line in csv.reader(data): print(line) # then data is read line by line # using csv.reader the printed # result will be in a list format # which is easy to interpret ... import csv filename ="Geeks.csv" # opening the file using "with" # statement with open(filename, 'r') as data: for line in csv.DictReader(data): print(line) ... from csv import DictReader # open file in read mode with open("geeks.csv", 'r') as f: dict_reader = DictReader(f) list_of_dict = list(dict_reader) print(list_of_dict)
🌐
Imperial College London
python.pages.doc.ic.ac.uk › java › lessons › java › 10-files › 09-csvreaddict.html
Python for Java Programmers > Reading CSV files into a dict | Department of Computing | Imperial College London
To make life easier, you can also read in the CSV files into a dict, using a csv.DictReader object. You can then access elements using the column names as keys (from the first row).
Find elsewhere
🌐
Linux Hint
linuxhint.com › use-python-csv-dictreader
Linux Hint – Linux Hint
December 2, 2021 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Python Beginners
python-adv-web-apps.readthedocs.io › en › latest › csv.html
CSV Files — Python Beginners documentation - Read the Docs
The csv.DictReader() method is used to convert a CSV file to a Python dictionary. You read from an existing CSV and create a Python dictionary from it.
🌐
Medium
medium.com › @3valuedlogic › using-python-csv-3-dictreader-e4814ce2e44
Using Python CSV #3 — DictReader. Python’s csv module allows you to work… | by David W. Agler | Medium
December 22, 2022 - The csv.DictReader is a function returns a DictReader object given a csv file. If we iterate over it, we see it contains dictionaries whose elements (keys and values) are strings.
🌐
PythonForBeginners.com
pythonforbeginners.com › home › read csv into list of dictionaries in python
Read CSV Into List of Dictionaries in Python - PythonForBeginners.com
July 22, 2022 - In python, we can use the csv module to work with csv files. To read a csv file into a list of dictionaries, we will create a csv.DictReader object using the csv.DictReader() method.
🌐
Saturn Cloud
saturncloud.io › blog › how-to-convert-a-csv-file-to-a-dictionary-in-python-using-the-csv-and-pandas-modules
How to Convert a CSV File to a Dictionary in Python using the CSV and Pandas Modules | Saturn Cloud Blog
May 1, 2026 - We create a csv.DictReader object to read the file and automatically convert each row into a dictionary. The data variable then holds a list of dictionaries, where each dictionary represents a row from the CSV file.
🌐
Kite
kite.com › python › answers › how-to-read-a-`.csv`-file-into-a-dictionary-in-python
Kite is saying farewell - Code Faster with Kite
November 20, 2022 - P.S. Most of our code has been open sourced on Github here. It includes our data-driven Python type inference engine, Python public-package analyzer, desktop software, editor integrations, Github crawler and analyzer, and much more.
🌐
Brodan
brodan.biz › blog › parsing-csv-files-with-python
Parsing CSV Files with Python's DictReader - Brodan.biz
August 24, 2018 - The DictReader class basically creates a CSV object that behaves like a Python OrderedDict. It works by reading in the first line of the CSV and using each comma separated value in this line as a dictionary key.
🌐
Codecademy Forums
discuss.codecademy.com › data science
What does .dictreader() do? - Data Science - Codecademy Forums
September 27, 2023 - hi guys! I have a question. I know when we use .dictreader() we use csv module to make a dictionary out of the csv file, so it’s easier to use for programming, but I don’t get it how does this dictionary look like exactly. first, I thought it uses titles in the first line to make keys, and then unzip the rest into these keys based on their index. but now, I see it like this, which doesn’t make sense. import csv with open("cool_csv.csv") as cool_csv_file : cool_csv_dict = csv.DictReader(coo...
🌐
Finxter
blog.finxter.com › home › learn python blog › convert csv to dictionary in python
Convert CSV to Dictionary in Python - Be on the Right Side of Change
August 13, 2022 - Here’s the code to convert that CSV file to multiple dictionaries, one dictionary per row by using the csv.DictReader(file) function:
🌐
Python Pool
pythonpool.com › home › blog › csv dicteader doesn’t have to be hard
CSV Dicteader Doesn't Have To Be Hard - Python Pool
December 18, 2021 - So, CSV.Dictreader creates a dictionary object for each row of the CSV file and maps each row value to the column name as the key. Keys and Values are stored as the string.
🌐
Java2Blog
java2blog.com › home › python › convert csv to dictionary in python
Convert CSV to Dictionary in Python - Java2Blog
January 6, 2023 - We parse the data using the DictReader class. Each row of this parsed data is of OrderedDict class · Each row of parsed data is converted to a dictionary using the dict() constructor. We append these dictionaries to a list and display it. In the previous section, we discovered how to convert CSV to dictionary in Python ...