Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

convert 2 lists to a dictionary in python

""" How to convert 2 lists into a dictionary in python """
# list used for keys in dictionary
students = ["Angela", "James", "Lily"]

# list of values
scores = [56, 76, 98]

# convert lists to dictionary
student_scores = dict(zip(students, scores))

# print the dictionary
print(student_scores)

""" result should be
student_scores: -{
        Angela: 56,
        James: 76,
        Lily: 98
    },
"""
Comment

how to convert multi list to dict

# Your Data list

names = ["john", "paul", "george", "ringo"]
job = ["guitar", "bass", "guitar", "drums"]
status = ["dead", "alive", "dead", "alive"]

dict_data = [{'name': name, 'job': job, 'status': status} for name,job,status in zip(names,jobs,statuses)]
Comment

create dict from two lists

keys = ['a', 'b', 'c']
values = [1, 2, 3]
dictionary = dict(zip(keys, values))
Comment

make dict from 2 lists

keys = ['a', 'b', 'c']
values = [1, 2, 3]
dictionary = dict(zip(keys, values))
print(dictionary) # {'a': 1, 'b': 2, 'c': 3}
Comment

create a dict from two lists

zip_iterator = zip(keys_list, values_list)
Comment

convert 2 lists into dictionary

# convert 2 list into dictionary
a = ['gsw','lakers','clippers']
b = [1,2,3]
my_dict = dict(zip(a,b))
print(my_dict)					# {'gsw': 1, 'lakers': 2, 'clippers': 3}
Comment

how to convert multi list into dict in python

from collections import OrderedDict

o = collections.OrderedDict()
for i in data:
     o.setdefault(i[0], []).append(i[1])
Comment

2 liste to a dictionary

d = {}
for i in list1:
    for j in list2:
        d[i] = j
print d
Comment

2 liste to a dictionary


keys = tel.keys()
values = tel.values()

Comment

python two list into dictinaray

index = [1, 2, 3]
languages = ['python', 'c', 'c++']

dictionary = dict(zip(index, languages))
print(dictionary)
Comment

PREVIOUS NEXT
Code Example
Python :: Multiple Box Plot using Seaborn 
Python :: python counter least common 
Python :: how to check if all characters in string are same python 
Python :: binary string to hex python 
Python :: chart-studio python install 
Python :: logging in with selenium 
Python :: display Surface quit 
Python :: python sum dictionary values by key 
Python :: isinstance float or int 
Python :: python unzip list 
Python :: post request python 
Python :: python get computer name 
Python :: python get day month year 
Python :: add text to the middle of the window tkinter 
Python :: Python - Count the Number of Keys in a Python Dictionary 
Python :: python palindrome string 
Python :: make directory python 
Python :: slack send message python 
Python :: python dataframe shape 
Python :: convert from 12 hrs to 24 python 
Python :: import pyplot python 
Python :: python class name 
Python :: bar plot matplotlib 
Python :: generic python 
Python :: Import CSV Files into R Using fread() method 
Python :: connect with pyodbc with statement 
Python :: show image python 
Python :: phone number regex python 
Python :: pandas df row count 
Python :: how to execute python program in ubuntu 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =