Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

list to dict python

my_list = ['Nagendra','Babu','Nitesh','Sathya']
my_dict = dict() 
for index,value in enumerate(my_list):
  my_dict[index] = value
print(my_dict)
#OUTPUT
{0: 'Nagendra', 1: 'Babu', 2: 'Nitesh', 3: 'Sathya'}
Comment

python convert a list to dict

#python convert a dict to list or a list to dict or a slice a dict or sort a dict by key or value without import
#convert dict to list or list to dict or slice a dict
#1. convert dict to list
temp = [] # not required as [] is mentioned below
s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}#our dict remember
temp = [[k,v]for k,v in s.items()]# temp is now list
print(temp)#[['b', 3], ['a', 2], ['c', 2], ['d', 1], ['e', 1]]

#2. list to dict || pass a list like below
a_dict ={}# not required since we have {} in below
a_dict = {  v[0]:v[1] for k,v in enumerate(temp)}
# OR || a_dict = {  v[0]:v[1] for k,v in enumerate([k,v]for k,v in s.items())}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}

#3. Slice a dict is as simple slice as slicing a list
#see below at temp[0:3] to slice dict to first 4 elements (0 to 3)
a_dict = {  v[0]:v[1] for k,v in enumerate(temp[0:3])}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2}
# OR Also like doing this ([[k,v]for k,v in s.items()][0:3])
#a_dict = { v[0]:v[1] for k,v in enumerate([[k,v]for k,v in s.items()][0:3])}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2}

#Sort a dict #Reverse True for desc and False for asc
#4. Sort a dict by key || s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}
a_dict = dict(sorted(s.items(),key=lambda x:x[0],reverse = False))
print(a_dict )# {'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
#Sort a dict by value || put 1 in x:x[1]
a_dict = dict(sorted(s.items(),key=lambda x:x[1],reverse = False))
#{'d': 1, 'e': 1, 'a': 2, 'c': 2, 'b': 3}

#5. Sort dict with import operator
import operator as op#change itemgetter to 0|1 for key|value
a_dict = dict(sorted(s.items(),key=op.itemgetter(0), reverse = False))
print(a_dict)#{'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
#refer to #python convert a dict to list or a list to dict or a slice a dict or sort a dict by key or value without import
#convert dict to list or list to dict or slice a dict
#1. convert dict to list
temp = [] # not required as [] is mentioned below
s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}#our dict remember
temp = [[k,v]for k,v in s.items()]# temp is now list
print(temp)#[['b', 3], ['a', 2], ['c', 2], ['d', 1], ['e', 1]]

#2. list to dict || pass a list like below
a_dict ={}# not required since we have {} in below
a_dict = {  v[0]:v[1] for k,v in enumerate(temp)}
# OR || a_dict = {  v[0]:v[1] for k,v in enumerate([k,v]for k,v in s.items())}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}

#3. Slice a dict is as simple slice as slicing a list
#see below at temp[0:3] to slice dict to first 4 elements (0 to 3)
a_dict = {  v[0]:v[1] for k,v in enumerate(temp[0:3])}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2}
# OR Also like doing this ([[k,v]for k,v in s.items()][0:3])
#a_dict = { v[0]:v[1] for k,v in enumerate([[k,v]for k,v in s.items()][0:3])}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2}

#Sort a dict #Reverse True for desc and False for asc
#4. Sort a dict by key || s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}
a_dict = dict(sorted(s.items(),key=lambda x:x[0],reverse = False))
print(a_dict )# {'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
#Sort a dict by value || put 1 in x:x[1]
a_dict = dict(sorted(s.items(),key=lambda x:x[1],reverse = False))
#{'d': 1, 'e': 1, 'a': 2, 'c': 2, 'b': 3}

#5. Sort dict with import operator
import operator as op#change itemgetter to 0|1 for key|value
a_dict = dict(sorted(s.items(),key=op.itemgetter(0), reverse = False))
print(a_dict)#{'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
#refer to https://ideone.com/uufYuP
#dn't forget to upvote
Comment

python list to dict

keys = ('name', 'age', 'food')
values = ('Monty', 42, 'spam')
out = dict(zip(keys, values))
Comment

dictionary to list python

# This is our example dictionary
petAges = {"Cat": 4, "Dog": 2, "Fish": 1, "Parrot": 5}
# This will be our list, epmty for now
petAgesList = []
# Search through the dictionary and find all the keys and values
for key, value in petAges.items(): 
  petAgesList.append([key, value]) # Add the key and value to the list
print(petAgesList) # Print the now-filled list
# Output: [['Cat', 4], ['Dog', 2], ['Fish', 1], ['Parrot', 5]]
Comment

python dictionary to list

for key, value in dict.iteritems():
    temp = [key,value]
    dictlist.append(temp)
Comment

python dictionary to list

>> d = {'a': 'Arthur', 'b': 'Belling'}

>> d.items()
[('a', 'Arthur'), ('b', 'Belling')]

>> d.keys()
['a', 'b']

>> d.values()
['Arthur', 'Belling']
Comment

python list to dict

keys = ('name', 'age', 'food')
values = ('Monty', 42, 'spam')
out = dict(zip(keys, values))
Comment

lists to dictionary python

#This is to convert two lists into a dictionary in Python

#given ListA is key
#given ListB is value

listA = ['itemA','itemB']
listB = ['priceA','priceB']
dict(zip(listA, listB))

# Returns 
{'itemA': 'priceA',
 'itemB': 'priceB'
}
Comment

python list of dictionaries to list

[d['value'] for d in l]
Comment

python list to dict

keys = ('name', 'age', 'food')
values = ('Monty', 42, 'spam')
out = dict(zip(keys, values))
Comment

python list to dict

keys = ('name', 'age', 'food')
values = ('Monty', 42, 'spam')
out = dict(zip(keys, values))
Comment

python list to dict

keys = ('name', 'age', 'food')
values = ('Monty', 42, 'spam')
out = dict(zip(keys, values))
Comment

list of dict to dict python

single_dict = dict((k,v) for k,v in [item for subtup in list_of_dict for item in subtup])
Comment

convert dictionary keys to list python

newlist = list()
for i in newdict.keys():
    newlist.append(i)
Comment

list to dictionary

b = dict(zip(a[::2], a[1::2]))
Comment

dict to list python

dict.items()
Comment

python list of dictionaries to list

[d['value'] for d in l if 'value' in d]
Comment

PREVIOUS NEXT
Code Example
Python :: python custom exception 
Python :: python module install a whl 
Python :: most popular python libraries 
Python :: Python NumPy repeat Function Example 
Python :: python request coinmarketcap 
Python :: python delete from dictionary 
Python :: json and python login system 
Python :: how to select rows with specific values in pandas 
Python :: dict get list of values 
Python :: how to exit program in python 
Python :: python datetime add 
Python :: reading doc in python 
Python :: while true python 
Python :: How to Crack PDF Files in Python - Python Cod 
Python :: python check phone number 
Python :: pandas concat 
Python :: even numbers from 1 to 100 in python 
Python :: how to run python file from cmd 
Python :: numpy get diagonal matrix from matrix 
Python :: looping on string with python 
Python :: python trim leading whitespace 
Python :: update_or_create django 
Python :: pandas today date 
Python :: model.fit(X_train, Y_train, batch_size=80, epochs=2, validation_split=0.1) 
Python :: counting combinations python 
Python :: socket get hostname of connection python 
Python :: print string and variable python 
Python :: to string python 
Python :: python mathematics 
Python :: training linear model sklearn 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =