Showing posts with label dict. Show all posts
Showing posts with label dict. Show all posts

3/14/2023

python dict shuffle

shuffle dict order 


.

import random

my_dict = {'apple': 2, 'banana': 3, 'orange': 1, 'kiwi': 4}

# Convert the dictionary to a list of tuples and shuffle it
items = list(my_dict.items())
random.shuffle(items)

# Convert the shuffled list back to a dictionary
shuffled_dict = {k: v for k, v in items}

print(shuffled_dict)

..


Thank you.


4/19/2022

Remove duplicated dict element in list, remove same dict element in tow list

 refer to code:


..

a=[{'a':1, 'b':3}, {'a':2, 'b':4}]
b=[{'a':3, 'b':3}, {'a':2, 'b':4}]
a.extend(b)
[dict(t) for t in {tuple(d.items()) for d in a}]

>> [{'a': 1, 'b': 3}, {'a': 2, 'b': 4}, {'a': 3, 'b': 3}]

..


Thank you.

www.marearts.com

3/08/2022

python dict keys to list

 refer to example code

..

dictionary = {"a": 1, "b": 2, "c": 3}
key_iterable = dictionary.keys()
key_list = list(key_iterable)
print(key_list)

..



www.marearts.com

Thank you. πŸ™‡πŸ»‍♂️