Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

create dictionary python from two lists

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

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

Convert two lists into a dictionary in Python

>>> students = ["Cody", "Ashley", "Kerry"]
>>> grades = [93.5, 95.4, 82.8]
>>> {s: g for s, g in zip(students, grades)}
{'Cody': 93.5, 'Ashley': 95.4, 'Kerry': 82.8}
Comment

create dict from two lists

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

python merge list of dict into single dict

l = [{'a': 0}, {'b': 1}, {'c': 2}, {'d': 3}, {'e': 4, 'a': 4}]
d = {k: v for x in l for k, v in x.items()}
print(d)
# {'a': 4, 'b': 1, 'c': 2, 'd': 3, 'e': 4}
Comment

Converting Two Lists Into a Dictionary

column_names = ['id', 'color', 'style']
column_values = [1, 'red', 'bold']

# Convert two lists into a dictionary with zip and the dict constructor
name_to_value_dict = dict(zip(column_names, column_values))

# Convert two lists into a dictionary with a dictionary comprehension
name_to_value_dict = {key:value for key, value in zip(column_names, column_values)}

# Convert two lists into a dictionary with a loop
name_value_tuples = zip(column_names, column_values) 
name_to_value_dict = {} 
for key, value in name_value_tuples: 
  if key in name_to_value_dict: 
    pass # Insert logic for handling duplicate keys 
  else: 
    name_to_value_dict[key] = value
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

dictionary from two list

feat_dic = dict(zip(X.columns, feat_imp))
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

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 :: plus in python 
Python :: list -1 python 
Python :: heroku requirements.txt python 
Python :: read and write to file python 
Python :: python read excel 
Python :: python split string after substring 
Python :: save object pickle python 
Python :: duplicate in list 
Python :: read data from excel and plot in python 
Python :: for loop from n to 1 in python 
Python :: how to insert item at specifc index python 
Python :: messagebox python pyqt 
Python :: get basename without extension python 
Python :: unique value python 
Python :: check if list elememnt in dataframe column 
Python :: count specific instances in a columb in pandas 
Python :: pytohn reset all dictionary values to 0 
Python :: download image from url python requests 
Python :: python isinstance 
Python :: CSV data source does not support array<string data type 
Python :: python two string equal 
Python :: beautifulsoup find element by partial text 
Python :: how to have player input in python 
Python :: how to convert pandas series to 2d numpy array 
Python :: python file write 
Python :: print input in python 
Python :: binary search python 
Python :: How to Adjust Title Position in Matplotlib 
Python :: Get the square root of a number in Python 
Python :: format column from string to numeric in python 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =