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

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 :: how to get a specific character in a string on number python 
Python :: production mode flask 
Python :: python search a string in another string get last result 
Python :: python raise filenotfounderror 
Python :: base64 python flask html 
Python :: python list intersection 
Python :: django many to many post update method via rest 
Python :: python dynamic variable name 
Python :: Removing Elements from Python Dictionary Using clear() method 
Python :: dm command in discord.py 
Python :: nrf24l01 arduino to raspberry pi struct 
Python :: pandas using eval converter excluding nans 
Python :: Python operator to use for set union 
Python :: python range from n to 0 
Python :: zip() python 
Python :: np.append 
Python :: get the last item in a python list 
Python :: python empty list 
Python :: 3d array 
Python :: dataframe select row by index value 
Python :: convert string to int python 
Python :: sort list in python 
Python :: Random Colored Shapes with python turtle 
Python :: web scraping with selenium 
Python :: is login a class in python 
Python :: Converting 12 hour clock time to 24 hour clock time 
Python :: clear 
Python :: how to access pandas column 
Python :: f string python 
Python :: python convert np datetime to string 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =