Python Forum
Creating a dictionary from a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a dictionary from a list
#1
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
Reply
#2
Do you know how to add a key/value to a dictionary?
Reply
#3
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/
Reply
#4
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'}
Inkanus likes this post
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.
Reply
#5
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}
Reply
#6
(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"
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionary in a list bashage 2 498 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 598 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  Sort a list of dictionaries by the only dictionary key Calab 1 453 Oct-27-2023, 03:03 PM
Last Post: buran
  How to add list to dictionary? Kull_Khan 3 951 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  check if element is in a list in a dictionary value ambrozote 4 1,885 May-11-2022, 06:05 PM
Last Post: deanhystad
  Dictionary from a list failed, help needed leoahum 7 1,902 Apr-28-2022, 06:59 AM
Last Post: buran
  how to assign items from a list to a dictionary CompleteNewb 3 1,536 Mar-19-2022, 01:25 AM
Last Post: deanhystad
  Python, how to manage multiple data in list or dictionary with calculations and FIFO Mikeardy 8 2,528 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,884 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Python dictionary with values as list to CSV Sritej26 4 2,959 Mar-27-2021, 05:53 PM
Last Post: Sritej26

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020