Preserve order from dictionary keys [duplicate] - python

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

Related

Why Python Dict is not printing the same order that inserted? [duplicate]

This question already has answers here:
Are dictionaries ordered in Python 3.6+?
(6 answers)
Closed 11 months ago.
I am using the following code to print keys and values of a dict.
for key, val in index1.items():
print("++++++++")
print ( key)
print (val)
print("++++++++")
I want it's to be printed in the same order as it was inserted. Currently, it's not following that.
It is not printing in the same order because the order is not important since you can't get any values with indexes because you use strings as keys to get values. But you can always use .sorted() if you want (it doesn't impact anything in the dictionary, just the printing part)

Duplicate Key in dictionary in python [duplicate]

This question already has answers here:
How can one make a dictionary with duplicate keys in Python?
(9 answers)
Python JSON parse duplicate records
(1 answer)
Closed 3 years ago.
Python: 3.7.3
I have a string which needs to be converted to dictionary.
So, I have achieved it using ast.literal_eval method.
my_string = "{'ReceiptMessageId':'foo','ReceiptMessageId':'boo','ReceiptMessageName':'zoo'}"
import ast
my_dict = ast.literal_eval(my_string)
But issue here is, string (my_string) has same key with different values and converted dictionary replaces with lastly received value.
Expected:
{'ReceiptMessageId': ['foo','boo'], 'ReceiptMessageName': 'zoo'}
Actual:
{'ReceiptMessageId': 'boo', 'ReceiptMessageName': 'zoo'}
After googling a lot, I found this can be achieved using defaultdict from collections but in my case, duplicates are already ignored while converting into dict. Can someone give me an idea as to how to go about it?

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))

How does Python sort user-defined objects? [duplicate]

This question already has answers here:
How does Python 2 compare string and int? Why do lists compare as greater than numbers, and tuples greater than lists?
(2 answers)
Closed 7 years ago.
What's the default order for sorting user-defined objects in Python? I know how to change this, using the key parameter or defining the __lt__ etc, methods, but not what order you get if you don't.
Python sorts objects by default in ascending order. A simple ascending sort is done by calling the sorted() function.
It returns a new sorted list.

Key-value map in python [duplicate]

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))

Categories

Resources