How do I convert list into dictionary in Python?

Ryan Oh

So I have this.
[{"what": "Drink", "allDay": true, "id": 12, "when": "2020-05-13", "end": "2020-05-13"}] I'm trying to print the value of "what", but I'm having trouble converting this list into dictionary. I simply need the brackets come off. How do I do this? Thank you. :)

CypherX

What you have is a list of dict(s). So, you could do it as follows.

  • If values is the name of list variable, then values[i] gives you access to a dictionary stored at index i.
  • The dict.get(key, None) will either return a value or None when the key does not exist. This may allow you to avoid unwanted error when a certain field is absent.
# Use of dict.get(key, "value-when-no-match")
# is safer than directly calling dict[key]
# BUT, this depends on your design/logic/requirements.
values[0].get('what', None)

An Observation

Since "allDay": true was in your sample data, I assume that most likely you are trying to read this from a JSON file. If that is the case, and if you are using json library in python, that could be easily dealt with using json.load(), json.loads().

Dummy Data

Note that you had a boolean value (for "allDay") as true >> Python expects True with a capital T.

values = [
    {
        "what": "Drink", 
        "allDay": True, 
        "id": 12, 
        "when": "2020-05-13", 
        "end": "2020-05-13"
    },

    {
        "what": "Eat", 
        "allDay": True, 
        "id": 14, 
        "when": "2020-05-14", 
        "end": "2020-05-14"
    }
]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert a list to dictionary with specific format in python?

How do I convert a list of lists to a python dictionary of the following format?

How do I convert a list/dictionary into a Dataframe?

How do I convert a list of tuples to a dictionary

How do I concisely convert a python dictionary to a list of tuples, given a dictionary in which the values may be different types?

How do I convert this dictionary to a dataframe in python?

How do I convert a list with values of different lengths into a dictionary?

How do i convert a dictionary with unequal list as value to csv

How do i convert list with word count into a dictionary

How do I convert a list into a dictionary grouped by object name?

python dictionary and list: how to convert it?

how to convert a list to dictionary in python?

How to convert list to dictionary in Python?

How can I convert a list to JSON/dictionary - Python

Reupload, How do i convert a php file to python dictionary?

How do I convert a csv file with one column to a dictionary in Python?

How to convert list of string to list of dictionary in Python

How to convert list of list to dictionary in Python?

How do I use numbers in a list as keys for my dictionary in Python?

How do I return dictionary keys as a list in Python?

How do I find the last item of the list in a Python dictionary?

Python: How do I remove elements from a dictionary and return it as a list?

How do I join a list of values into a Python dictionary?

How do I remove a dictionary index from a list on Python?

How do I append a list to an existing dictionary in python

How do I access a certain list entry in a dictionary in python?

How do I remove string '' from python dictionary list values?

how do i convert nested dictionary within a dictionary to dictionary

How do you convert a Dictionary to a List?