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

merge two list of dictionaries python with string

import pandas as pd

l1 = [{'id': 9, 'av': 4}, {'id': 10, 'av': 0}, {'id': 8, 'av': 0}]
l2 = [{'id': 9, 'nv': 45}, {'id': 10, 'nv': 0}, {'id': 8, 'nv': 30}]

df1 = pd.DataFrame(l1).set_index('id')
df2 = pd.DataFrame(l2).set_index('id')
df = df1.merge(df2, left_index=True, right_index=True)
df.T.to_dict()
# {9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}
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 can i plot graph from 2 dataframes in same window python 
Python :: voice translate python 
Python :: PackagesNotFoundError: The following packages are not available from current channels: 
Python :: adding roles discord py 
Python :: sqlalchemy create engine MySQL 
Python :: pyqt5 image center 
Python :: how do i get parent directory python 
Python :: split a string into N equal parts. 
Python :: msg.author discord.py 
Python :: python format 001 
Python :: if statement in one-line for loop python 
Python :: how to write a script to display an image in python 
Python :: how to loop through string in python 
Python :: python cv2 imwrite 
Python :: how print 2 decimal in python 
Python :: how to take input for list in one line in python 
Python :: print first word of a string python and return it 
Python :: how to for loop for amount of characters in string python 
Python :: feature to determine image too dark opencv 
Python :: how to restart loop python 
Python :: python execute function from string 
Python :: print in python without using print or sys module 
Python :: python see if a number is greater than other 
Python :: python create a grid of points 
Python :: cardano 
Python :: how to encrypt text in python 
Python :: get_absolute_url django 
Python :: how to get all possible combinations in python 
Python :: python simple web app 
Python :: pandas melt() function, change the DataFrame format from wide to long 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =