Python - function to create a dictionary from a list

python dictionary
python create dictionary from list
python create dictionary from list of values
python convert list to dictionary keys
python dictionary with list as value
python create dictionary from two lists
convert list of key-value pairs to dictionary python
python convert list to dictionary with index

I'm defining a function in Python that takes a list as an argument. The function should return a dictionary from that list.

persons = [['john','doe'],['tony','stark']]

def build_agenda(person_list):
    """Return a dictionary about a list of information of people"""
    persons = {}
    for person in person_list:
        persons['first_name'] = person[0]
        persons['last_name'] = person[1]
    return persons

output = build_agenda(persons)
print(output)

The problem is that only one value it's being returned as a dictionary, isn't the code supposed to create a new entry for each person that it's found on the list?

You only ever create one single dictionary, regardless of how many people are in person_list. You want to create one dictionary per person. A dictionary's keys must be unique, so your for-loop simply overwrites the previous key-value pairs with the most recent one, so when you return persons, you're just returning a single dictionary containing the last person's information.

persons = [["John", "Doe"], ["Tony", "Stark"]]

dicts = [dict(zip(("first_name", "last_name"), person)) for person in persons]
print(dicts)

Output:

[{'first_name': 'John', 'last_name': 'Doe'}, {'first_name': 'Tony', 'last_name': 'Stark'}]

dicts in this case, is a list of dictionaries, one for each person.

Python, Now we want to create a dictionary with all elements in this list as keys. For each key Converting a list to dictionary with list elements as keys in dictionary. All keys will have Python : map() function explained with examples� How to create Python dictionary from list of keys and values? Python Server Side Programming Programming If L1 and L2 are list objects containing keys and respective values, following methods can be used to construct dictionary object.

Similar to @user10987432, but I don't like using dict because it's slow.

You could write it instead like:

persons = [['john','doe'],['tony','stark']]

def build_agenda(person_list):
    """Return a list of dictionaries about a list of information of people"""
    persons = [{'first_name': first, 'last_name': last} 
               for first, last in persons]
    return persons

output = build_agenda(persons)
print(output)

Python : How to convert a list to dictionary ? – thispointer.com, If L1 and L2 are list objects containing keys and respective values, following Zip two lists and convert to dictionary using dict() function >� Similarly, using one for loop, read and store the values for the second list in secondlist_ variable. Print out both lists to the user. Using zip(), create one list of pairs from the lists. Then using dict(), convert that list of pairs to a dictionary. Print out the created dictionary.

In addition to the solution mentioned above, if you really need dict of dicts, you can go this way and build nested dict:

persons = [['john','doe'],['tony','stark']]    
result = {idx: {'first_name': person[0], 'last_name': person[1]} for idx, person in enumerate(persons)}

This will give you:

{0: {'first_name': 'john', 'last_name': 'doe'}, 1: {'first_name': 'tony', 'last_name': 'stark'}}

How to create Python dictionary from list of keys and values?, How to create a dictionary with key value pairs from a list containing duplicates? Show less Duration: 6:09 Posted: Dec 26, 2015 Create a dictionary with list comprehension in Python. I like the Python list comprehension syntax. Can it be used to create dictionaries too? For example, by iterating over pairs of keys and values: mydict = {(k,v) for (k,v) in blah blah blah} You're looking for the phrase "dict comprehension" - it's actually: mydict = {k: v for k, v in iterable}

Python Programming: Creating a dictionary from a list, Use a dict comprehension: {key: value for (key, value) in iterable}. Note: this is for Python 3.x (and 2.7 upwards). Formerly in Python 2.6 and� To convert a list to dictionary, we can use list comprehension and make a key:value pair of consecutive elements. Finally, typecase the list to dict type. First create an iterator, and intialise it to variable ‘it’. Then use zip method, to zip keys and values together. Finally typecast it to dict type.

Create a dictionary with list comprehension in Python, You can define a dictionary by enclosing a comma-separated list of key-value pairs in You can also construct a dictionary with the built-in dict() function. Once the dictionary is created in this way, its values are accessed the same way as� 7. In-Built Functions on a Python Dictionary. A function is a procedure that can be applied on a construct to get a value. Furthermore, it doesn’t modify the construct. Python gives us a few functions that we can apply on a Python dictionary. Take a look. a. len() The len() function returns the length of the dictionary in Python.

Dictionaries in Python – Real Python, In Python, dictionaries are written using curly brackets. Each key-value pair python. Figure 2 - Using the .get() method to prevent key errors. Of course Figure 4 - Creating a list and adding new elements. Another built-in� ''' Converting a list to dictionary with list elements as keys in dictionary using dict.fromkeys() ''' dictOfWords = dict.fromkeys(listOfStr , 1) dict.fromKeys() accepts a list and default value. It returns a dictionary with items in list as keys.

Comments
  • Don't put text as image
  • You are overriding the dictionary's 'first_name' and 'last_name' on every iteration of the loop after the first.
  • On each iteration you are redefining the value of same keys in a dict (last_name and first_name).