Convert items in the Python list to dictionary

Asked 2 years ago, Updated 2 years ago, 128 views

a=["Seoul", "Seoul", "Gyeonggi", "Gyeonggi", "Incheon", "Incheon"]

dic={0: 'Seoul', 1: 'Seoul', 2: 'Gyeonggi', 3: 'Gyeonggi', 4: 'Incheon', 5: 'Incheon'}

How do I change from a to dic?

list dictionary

2022-09-22 08:17

1 Answers

In [2]: a=[Seoul, Seoul, Gyeonggi, Gyeonggi, Incheon, Incheon]
In [3]: dict(zip(range(len(a)), a))
Out[3]: {0: 'Seoul', 1: 'Seoul', 2: 'Gyeonggi', 3: 'Gyeonggi', 4: 'Incheon', 5: 'Incheon'}


2022-09-22 08:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.