Hướng dẫn ordereddict.fromkeys python

Hàm Dictionary fromkeys[] trong Python được sử dụng để tạo một Dictionary mới từ dãy seq và value. Trong đó dãy seq tạo nên các key và tất cả các key chia sẻ các giá trị từ value. Trong trường hợp value không được cung cấp thì value của các key là None.

Nội dung chính

  • Cú pháp
  • Ví dụ 1: tạo một Dictionary từ một tập các keys
  • Ví dụ 2: tạo một Dictionary từ một tập các keys với value
  • Ví dụ 3: tạo một Dictionary từ một tập các keys với list đối tượng có thể thay đổi

Cú pháp

Cú pháp của fromkeys[] trong Python:

dict.fromkeys[seq[, value]]]

Tham số:

  • seq: Đây là danh sách các value mà sẽ được sử dụng cho key trong Dictionary.

  • value: Tham số này là tùy ý, nếu được cung cấp thì value sẽ được thiết lập theo tham số này.


Ví dụ 1: tạo một Dictionary từ một tập các keys

Ví dụ sau minh họa cách sử dụng của fromkeys[] trong Python.

# keys nguyen am
keys = {'a', 'e', 'i', 'o', 'u' }
dict1 = dict.fromkeys[keys]
print[dict1]

Chạy chương trình Python trên sẽ cho kết quả:

{'o': None, 'u': None, 'i': None, 'e': None, 'a': None}

Ví dụ 2: tạo một Dictionary từ một tập các keys với value

Ví dụ sau minh họa cách sử dụng của fromkeys[] trong Python.

# keys nguyen am
keys = {'a', 'e', 'i', 'o', 'u' }
value = 'nguyen am'
dict1 = dict.fromkeys[keys, value]
print[dict1]

Chạy chương trình Python trên sẽ cho kết quả:

{'u': 'nguyen am', 'e': 'nguyen am', 'a': 'nguyen am', 'o': 'nguyen am', 'i': 'nguyen am'}


Ví dụ 3: tạo một Dictionary từ một tập các keys với list đối tượng có thể thay đổi

Ví dụ sau minh họa cách sử dụng của fromkeys[] trong Python.

# keys nguyen am
keys = {'a', 'e', 'i', 'o', 'u' }
value = [1]

dict1 = dict.fromkeys[keys, value]
print[dict1]

value.append[2]
print[dict1]

Chạy chương trình Python trên sẽ cho kết quả:

{'i': [1], 'o': [1], 'e': [1], 'u': [1], 'a': [1]}
{'i': [1, 2], 'o': [1, 2], 'e': [1, 2], 'u': [1, 2], 'a': [1, 2]}

The fromkeys[] method creates a dictionary from the given sequence of keys and values.

Example

# keys for the dictionary
alphabets = {'a', 'b', 'c'}

# value for the dictionary
number = 1

# creates a dictionary with keys and values dictionary = dict.fromkeys[alphabets, number]

print[dictionary] # Output: {'a': 1, 'c': 1, 'b': 1}

fromkeys[] Syntax

The syntax of the fromkeys[] method is:

dict.fromkeys[alphabets,number]

Here, alphabets and numbers are the key and value of the dictionary.

fromkeys[] Parameters

The fromkeys[] method can take two parameters:

  • alphabets - are the keys that can be any iterables like string, set, list, etc.
  • numbers [Optional] - are the values that can be of any type or any iterables like string, set, list, etc.

Note: The same value is assigned to all the keys of the dictionary.

fromkeys[] Return Value

The fromkeys[] method returns:

  • a new dictionary with the given sequence of keys and values

Note: If the value of the dictionary is not provided, None is assigned to the keys.

Example 1: Python Dictionary fromkeys[] with Key and Value

# set of vowels
keys = {'a', 'e', 'i', 'o', 'u' }

# assign string to the value
value = 'vowel'

# creates a dictionary with keys and values vowels = dict.fromkeys[keys, value]

print[vowels]

Output

{'a': 'vowel', 'u': 'vowel', 'e': 'vowel', 'i': 'vowel', 'o': 'vowel'}

In the above example, we have used the fromkeys[] method to create the dictionary with the given set of keys and string value.

Here, the fromkeys[] method creates a new dictionary named vowels from keys and value. All the keys of the dictionary are assigned with the same value.

Example 2: fromkeys[] without Value

# list of numbers
keys = [1, 2, 4 ]

# creates a dictionary with keys only numbers = dict.fromkeys[keys]

print[numbers]

Output

{1: None, 2: None, 4: None}

In the above example, we have created a dictionary named numbers with the given list of keys.

We have not provided any values, so all the keys are assigned None by default.

Example 3: fromkeys[] To Create A Dictionary From Mutable Object

# set of vowels
keys = {'a', 'e', 'i', 'o', 'u' }

# list of number
value = [1]

vowels = dict.fromkeys[keys, value]
print[vowels]

# updates the list value
value.append[2]

print[vowels]

Output

{'a': [1], 'u': [1], 'o': [1], 'e': [1], 'i': [1]}
{'a': [1, 2], 'u': [1, 2], 'o': [1, 2], 'e': [1, 2], 'i': [1, 2]}

In the above example, we have used a list as the value for the dictionary. The iterables like list, dictionary, etc are the mutable objects, meaning they can be modified.

Here, when we update the list value using append, the keys are assigned with the new updated values. This is because each element is pointing to the same address in the memory.

To solve this problem, we can use dictionary comprehension.

Dictionary comprehension for mutable objects

We can use dictionary comprehension and prevent updating the dictionary when the mutable object [list, dictionary, etc] is updated. For example,

# vowels keys
keys = {'a', 'e', 'i', 'o', 'u' }
value = [1]

# creates dictionary using dictionary comprehension vowels = { key : list[value] for key in keys }

print[vowels] # updates the value list value.append[2] print[vowels]

Output

{'a': [1], 'u': [1], 'o': [1], 'e': [1], 'i': [1]}
{'a': [1], 'u': [1], 'o': [1], 'e': [1], 'i': [1]}

In the above example, we have used dictionary comprehension to create a dictionary named vowels.

Here, the value is not assigned to the keys of the dictionary. But, for each key in keys, a new list of value is created. The newly created list is assigned each key in the dictionary.

Recommended Reading: Python Dictionary Comprehension.

Chủ Đề