Jemma Day
Jemma Day

Reputation: 17

Converting a list with multiple values into a dictionary

I have the following list and am wanting to convert it into a dictionary where the 4 digit value at the start of each item becomes the id.

['3574,A+,2021-03-24', '3575,O+,2021-04-03', '3576,AB-,2021-04-09', '3580,AB+,2021-04-27', '3589,A+,2021-05-08', '3590,B-,2021-05-11']

I have tried many different methods but it doesn't seem to work.

Upvotes: 0

Views: 72

Answers (4)

ThePyGuy
ThePyGuy

Reputation: 18426

You can use str.split, map and dictionary comprehension

# data holds the list you have provided
{splitted[0]:splitted[1:] for splitted in map(lambda item:item.split(','), data)}

OUTPUT:

Out[35]: 
{'3574': ['A+', '2021-03-24'],
 '3575': ['O+', '2021-04-03'],
 '3576': ['AB-', '2021-04-09'],
 '3580': ['AB+', '2021-04-27'],
 '3589': ['A+', '2021-05-08'],
 '3590': ['B-', '2021-05-11']}

Upvotes: 1

Nk03
Nk03

Reputation: 14949

TRY:

result = dict(i.split(',', 1) for i in lst)

OUTPUT:

{'3574': 'A+,2021-03-24',
 '3575': 'O+,2021-04-03',
 '3576': 'AB-,2021-04-09',
 '3580': 'AB+,2021-04-27',
 '3589': 'A+,2021-05-08',
 '3590': 'B-,2021-05-11'}

Upvotes: 0

Party-with-Programming
Party-with-Programming

Reputation: 301

Here is the code to do what I believe you asked for. I have also added comments in the code for a bit more clarification.

my_list = ['3574,A+,2021-03-24',
       '3575,O+,2021-04-03',
       '3576,AB-,2021-04-09',
       '3580,AB+,2021-04-27',
       '3589,A+,2021-05-08',
       '3590,B-,2021-05-11']#your list
my_dict = {}#The dictionary you want to put the list into

print("Your list:", my_list, "\n")

for item in my_list:#cycles through every item in your list  
    ID, value = item.split(",", 1)#Splits the item in your list only once (when it sees the first comma)
    print(ID + ": " + value)
    my_dict[ID] = value#Add the ID and value to your dictionary

print("\n" + "Your desired dictionary:", my_dict)

Which outputs this:

Your list: ['3574,A+,2021-03-24', '3575,O+,2021-04-03', '3576,AB-,2021-04-09', '3580,AB+,2021-04-27', '3589,A+,2021-05-08', '3590,B-,2021-05-11'] 

3574: A+,2021-03-24
3575: O+,2021-04-03
3576: AB-,2021-04-09
3580: AB+,2021-04-27
3589: A+,2021-05-08
3590: B-,2021-05-11

Your desired dictionary: {'3574': 'A+,2021-03-24', '3575': 'O+,2021-04-03', '3576': 'AB-,2021-04-09', '3580': 'AB+,2021-04-27', '3589': 'A+,2021-05-08', '3590': 'B-,2021-05-11'}

Enjoy!

Upvotes: 0

Andrej Kesely
Andrej Kesely

Reputation: 195508

You can use dictionary comprehension with str.split:

lst = [
    "3574,A+,2021-03-24",
    "3575,O+,2021-04-03",
    "3576,AB-,2021-04-09",
    "3580,AB+,2021-04-27",
    "3589,A+,2021-05-08",
    "3590,B-,2021-05-11",
]

out = {int(v.split(",")[0]): v.split(",")[1:] for v in lst}
print(out)

Prints:

{
    3574: ["A+", "2021-03-24"],
    3575: ["O+", "2021-04-03"],
    3576: ["AB-", "2021-04-09"],
    3580: ["AB+", "2021-04-27"],
    3589: ["A+", "2021-05-08"],
    3590: ["B-", "2021-05-11"],
}

Upvotes: 0

Related Questions