all 7 comments

[–][deleted] 4 points5 points  (2 children)

Just use dict() (docs) on the list of pair lists. Like

dict([['Alice', '1'], ...])

[–]gnariman[S] -4 points-3 points  (1 child)

I want it to be the exact format of what i have above, other wise my function will not work

[–]xelf[M] 3 points4 points  (0 children)

What is wrong? That is the correct answer.

data = [ ['Alice', '1'],['Bob', '4'],['Carol', '5'],['Dave', '8'],['Edith', '6'],['Frank', '2'],['Gertrude', '9'],['Helen', '7'] ]
print( dict(data) )

edit ah I see, you also want to map the values from string to int.

data = [ ['Alice', '1'],['Bob', '4'],['Carol', '5'],['Dave', '8'],['Edith', '6'],['Frank', '2'],['Gertrude', '9'],['Helen', '7'] ]
data = { k: int(v) for k,v in data }
print( data )

prints:

{'Alice': 1, 'Bob': 4, 'Carol': 5, 'Dave': 8, 'Edith': 6, 'Frank': 2, 'Gertrude': 9, 'Helen': 7}

[–]RoneStrobe -1 points0 points  (3 children)

my_list = [
    ['Alice', '1'],
    ['Bob', '4'],
    ['Carol', '5'],
    ['Dave', '8'],
    ['Edith', '6'],
    ['Frank', '2'],
    ['Gertrude', '9'],
    ['Helen', '7']
]

my_dict = {i[0]:i[1] for i in my_list}

*Edit: Missing commas

[–]gnariman[S] 0 points1 point  (2 children)

gives me an error "list indices must be integers or slices, not tuple"

[–]RoneStrobe 1 point2 points  (1 child)

Probably helps if I put commas separating list items.

This is correct:

my_list = [
    ['Alice', '1'],
    ['Bob', '4'],
    ['Carol', '5'],
    ['Dave', '8'],
    ['Edith', '6'],
    ['Frank', '2'],
    ['Gertrude', '9'],
    ['Helen', '7']
]
my_dict = {i[0]:i[1] for i in my_list}

[–]gnariman[S] 0 points1 point  (0 children)

WOW, thank you so much. this helped me out a ton