Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

I'm totally new to python and totally confused with .formkeys() with lists.

Look at my code:

    # dictionary exercise 4: Initialize dictionary with default values

employees = ['Kelly', 'Emma', 'John']

defaults = {"designation": 'Application Developer', "salary": 8000}

def_dictionary = dict()

def_dictionary.setdefault("designation", "Application Developer")

def_dictionary.setdefault("salary", 8000)

print(def_dictionary)

res_dict = dict.fromkeys(employees[0], defaults)

print(res_dict)

    print(res_dict)

Output:

{'K': {'designation': 'Application Developer', 'salary': 8000}, 'e': {'designation': 'Application Developer', 'salary': 8000}, 'l': {'designation': 'Application Developer', 'salary': 8000}, 'y': {'designation': 'Application Developer', 'salary': 8000}}

What I need to do is pair worker "Kelly" with the default esteems dictionary, notwithstanding, I don't comprehend why I get 'K', 'E', 'L', 'Y' strings as keys to my res_dict.

res_dict = dict.fromkeys(employees, defaults)

I don't know why my code gives kelly to 'K','E','L','Y'.

Thank you

closed

4 Answers

0 votes
by (19k points)
 
Best answer
The issue in your code stems from using dict.fromkeys() method with a single element instead of the entire list. Instead, pass the employees list as the first argument to dict.fromkeys() to pair each employee with the default values. Here's the code:

employees = ['Kelly', 'Emma', 'John']

defaults = {"designation": 'Application Developer', "salary": 8000}

res_dict = dict.fromkeys(employees, defaults)

print(res_dict)

With this modification, the output will be:

{'Kelly': {'designation': 'Application Developer', 'salary': 8000}, 'Emma': {'designation': 'Application Developer', 'salary': 8000}, 'John': {'designation': 'Application Developer', 'salary': 8000}}

Now, each employee name in the employees list is correctly paired with the defaults dictionary.
0 votes
by (26.4k points)

Here, employee[0] is a string, "kelly"

str objects are iterable - it will give you each character in sequence

For example:

for c in "kelly":

     print(c)

Which produces:

k

e

l

l

y

You will get a key for an each character in "kelly", when you call the function dict.fromkeys("Kelly",None)

Join the python online course fast, to learn python concepts in detail and get certified.

0 votes
by (25.7k points)
The issue in your code arises from the use of dict.fromkeys() method. The dict.fromkeys() method sets the keys of the dictionary to the elements of the iterable passed as the first argument, and the values for all keys are set to the same value (in this case, defaults).

In your code, when you pass employees[0] as the iterable to dict.fromkeys(), it treats the string 'Kelly' as an iterable of characters. Consequently, it sets each character ('K', 'e', 'l', 'l', 'y') as a separate key in the resulting dictionary with the value of defaults.

To achieve the desired result of pairing "Kelly" with the default values, you should pass the entire employees list as the iterable to dict.fromkeys(). Here's the corrected code:

employees = ['Kelly', 'Emma', 'John']

defaults = {"designation": 'Application Developer', "salary": 8000}

res_dict = dict.fromkeys(employees, defaults)

print(res_dict)

With this correction, the output will be:

{'Kelly': {'designation': 'Application Developer', 'salary': 8000}, 'Emma': {'designation': 'Application Developer', 'salary': 8000}, 'John': {'designation': 'Application Developer', 'salary': 8000}}

Now, each employee name in the employees list is paired with the defaults dictionary as desired.
0 votes
by (15.4k points)
In your code, the issue arises from the usage of the dict.fromkeys() method. This method sets the keys of the dictionary to the elements of the iterable passed as the first argument, while assigning the same value to all keys.

In your specific case, when you pass employees[0] (which is 'Kelly') as the iterable to dict.fromkeys(), it interprets the string as an iterable of characters. As a result, each character ('K', 'e', 'l', 'l', 'y') becomes a separate key in the resulting dictionary, all mapped to the same value (defaults).

To resolve this, ensure that you pass the entire employees list as the iterable to dict.fromkeys(). This way, each employee name will be paired with the defaults dictionary correctly.

Here's the code:

employee = ['Kelly', 'Emma', 'John']

defaults = {"designation": 'Application Developer', "salary": 8000}

res_dict = dict.fromkeys(employees, defaults)

print(res_dict)

With this modification, the output will be:

{'Kelly': {'designation': 'Application Developer', 'salary': 8000}, 'Emma': {'designation': 'Application Developer', 'salary': 8000}, 'John': {'designation': 'Application Developer', 'salary': 8000}}

Now, each employee name in the employees list is correctly associated with the defaults dictionary.

Related questions

Browse Categories

...