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

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

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 :: get common elements from two lists 
Python :: minimal flask application import 
Python :: python plot frequency of column values 
Python :: python dataframe rename first column 
Python :: pygame rect collisions 
Python :: convert list of strings to ints python 
Python :: django flash message 
Python :: how to loop through dates in python 
Python :: python hide console 
Python :: pytorch summary model 
Python :: ERROR: character with byte sequence 0xd0 0x9f in encoding "UTF8" has no equivalent in encoding "LATIN1" 
Python :: export image png python 
Python :: python reload import 
Python :: Python project root dir 
Python :: python create uuid 
Python :: python randomly shuffle rows of pandas dataframe 
Python :: python find smallest element in dictionary 
Python :: pandas add days to date 
Python :: how to get ip address of pc using python 
Python :: How to config your flask for gmail 
Python :: argparse boolean default 
Python :: os.system return value 
Python :: classification report scikit 
Python :: python underscore variable 
Python :: rmse in python 
Python :: password generator python 
Python :: pandas reset row indices 
Python :: print random string from list python 
Python :: How to get random int between two numbers python 
Python :: unlimited arguments python 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =