Key-value map in python [duplicate] - python

This question already has answers here:
How can I make a dictionary (dict) from separate lists of keys and values?
(21 answers)
Closed 7 years ago.
I have two list of same length l1=[reboot,revision,disk_space] and l2=[no,12.15,4300] I want to make these two list as key-value map. Output should be like this [reboot:no,revision:12.18,disk_space:4300].. I tried Zip it is not working

You can create a dictionary from the output of zip (Thanks, Martijn!):
print dict(zip(l1, l2))

Related

How to convert list object to string list [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 10 months ago.
I have problem with converting
[['a'],['b'],['c']] --> ['a','b','c']
it seems simple but I am struggling for hours...
You could use a list comprehension to do something as simple as
[element[0] for element in letters_list]
This remakes the list, extracting the 0-index element from each sublist

list comprehension with python dictionaries in python3 [duplicate]

This question already has answers here:
How can I get list of values from dict?
(7 answers)
Why does map return a map object instead of a list in Python 3?
(4 answers)
Closed 3 years ago.
I have recently started using python 3 and while working on list comprehension I am seeing something completely different from python 2. I want to know why is it that while printingthe value of 'y' python3 is returning [dict_values([1]), dict_values([2])] instead of [1,2] and how I can get response in desired form i.e,[1,2]
>>> x=[{'x':1},{'y':2}]
>>> y=[i.values() for i in x]
>>> y
[dict_values([1]), dict_values([2])]

How could I remove adjacent duplicate value without using a loop? [duplicate]

This question already has answers here:
Remove adjacent duplicate elements from a list [duplicate]
(17 answers)
Closed 3 years ago.
Like the given list is
l=[4,2,3,4,4,5]
Turn into
l=[4,2,3,4,5]
I try to create an empty list to store the value but it is hard to compare if the two elements are equal with no loop
to store every element only once is same like a set.
you can use following code.
l=[2,3,4,4,5]
l=list(set(l))
l.sort()
it will give your expected output
You can use a dictionary which only keeps unique values as its key
l=[2,3,4,4,5]
list(dict.fromKeys(l))

Preserve order from dictionary keys [duplicate]

This question already has answers here:
Order of keys in dictionaries in old versions of Python
(6 answers)
Closed 3 years ago.
I want to extract the dictionary keys, preserving their order (CTY, LC, DIV). When I perform a list comprehension, it is automatically ordering them.
columns = [key for key in dict_columns.keys()]
Any suggestions?
consider using an OrderedDict to preserve the order of keys in your dict
https://docs.python.org/3.7/library/collections.html#collections.OrderedDict

Append something into interior list within nested list [duplicate]

This question already has answers here:
How to insert into python nested list
(3 answers)
Closed 5 years ago.
so I have a nested list like this:
nested list=[['Bike No.,Purchase Date,Batt %,Last Maintenance,KM since Last'], ['T101,10/04/2016,55,10/01/2017,25.08'], ['T102,01/07/2016,10,15/05/2017,30.94'], ['T103,15/11/2016,94,13/06/2017,83.16'], ['T104,25/04/2017,58,10/01/2017,25.08'], ['T105,24/05/2017,5,20/06/2017,93.80']]
and I want to append another element into this list ['Bike No.,Purchase Date,Batt %,Last Maintenance,KM since Last'] ,how should I do it?
This list is originally created from a csv file and is split by its line followed by its elements
by coldspeed:
nested_list[0].append(...)

Categories

Resources