Posts: 13
Threads: 7
Joined: Oct 2020
Hello. I'm having trouble creating a dictionary from a list. I have a list with names. I am supposed to build a dictionary on its basis. Each name has to be assigned the number of characters for the name. Using 'len', John is 4 characters. I have a list
name_list = ["John", "Michael", "Terry", "Eric", "Graham"] Now I don't know how to make a dictionary out of this list. I did something like this but it's a collection.
name_dictionary = {'John', 'Michael', 'Terry', 'Eric', 'Graham'}
for i in name_dictionary:
print(i, len(i)) Generally, there is to be a list first, and then a dictionary from that list. Does anyone know how to do this? best regards
Posts: 6,812
Threads: 20
Joined: Feb 2020
Do you know how to add a key/value to a dictionary?
Posts: 1,034
Threads: 16
Joined: Dec 2016
Nov-05-2020, 11:31 PM
(This post was last modified: Nov-05-2020, 11:31 PM by Axel_Erfurt.)
name_list = ["John", "Michael", "Terry", "Eric", "Graham"]
name_dictionary = { i : name_list[i] for i in range(0, len(name_list) ) }
print(name_dictionary)
for v in name_dictionary.items():
print(v) Output: {0: 'John', 1: 'Michael', 2: 'Terry', 3: 'Eric', 4: 'Graham'}
(0, 'John')
(1, 'Michael')
(2, 'Terry')
(3, 'Eric')
(4, 'Graham')
look at https://thispointer.com/python-how-to-co...ictionary/
Posts: 1,950
Threads: 8
Joined: Jun 2018
If I correctly understand the objective then I would do:
>>> name_list = ["John", "Michael", "Terry", "Eric", "Graham"]
>>> d = dict(enumerate(name_list))
>>> d
{0: 'John', 1: 'Michael', 2: 'Terry', 3: 'Eric', 4: 'Graham'}
>>> d = dict(enumerate(name_list, start=1))
>>> d
{1: 'John', 2: 'Michael', 3: 'Terry', 4: 'Eric', 5: 'Graham'}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 6,812
Threads: 20
Joined: Feb 2020
From the original post:
Quote: Each name has to be assigned the number of characters for the name.
name_list = ["John", "Michael", "Terry", "Eric", "Graham"]
name_len = {name:len(name) for name in name_list}
Posts: 2,128
Threads: 11
Joined: May 2017
Nov-06-2020, 06:11 PM
(This post was last modified: Nov-06-2020, 06:11 PM by DeaD_EyE.)
(Nov-05-2020, 10:38 PM)Inkanus Wrote: name_dictionary = {'John', 'Michael', 'Terry', 'Eric', 'Graham'}
for i in name_dictionary:
print(i, len(i))
name_dictionary is a set and not a dictionary.
# this is a dictionary
color_mapping = {'blue': (0,0,255), 'green': (0,255,0), 'red': (255,0,0)}
color_keys = list(color_mapping) # implicit keys
color_keys = list(color_mapping.keys()) # it's explicit
color_values = list(color_mapping.values())
color_items = list(color_mapping.items())
print(color_keys)
print(color_values)
print(color_items) Adding a value to a key of an existing dict:
my_dict = {} # empty curly braces == dict
my_dict["a_string"] = "some value asigned to key a_string"
|