-2

I am learning python,

I am trying to merge keys['One', 'Two', 'Three'] and values [1, 2, 3] so I can get

a form of ==>: {'One': 1, 'Two': 2, 'Three': 3}

but it's not working with me in the below code.

Plus what is the difference if it was a form of ==>: ['One': 1, 'Two': 2, 'Three': 3] ?

please post the working code and explain how it works, try to make it works with long sets to.

keys = ['One', 'Two', 'Three']
values = [1, 2, 3]

after=values.join(keys)

print(after)
joe souaid
  • 40
  • 6

2 Answers2

1

For sets:

keys = ['One', 'Two', 'Three']
values = [1, 2, 3]
after = set(zip(keys, values))
print(after)

Result: {('One', 1), ('Three', 3), ('Two', 2)}

For dicts:

keys = ['One', 'Two', 'Three']
values = [1, 2, 3]
after = dict(zip(keys, values))
print(after)

Result: {'One': 1, 'Two': 2, 'Three': 3}

Using the zip function you can combine two lists and set one list as the key and another list as your values as you seem to want to do.

Treatybreaker
  • 776
  • 5
  • 9
1

[ 'one' : 1, 'two' : 2 ] this isn’t proper python syntax, if you want a list of key-value pairs, you need a list of tuples, like this [('one',1), ('two',2)].

faressalem
  • 574
  • 6
  • 20