HomeLinuxInitialize a Dictionary in Python

Initialize a Dictionary in Python


In Python, dictionaries are a set of unordered gadgets containing pairs of keys and values. Dictionaries are little bit totally different with regards to their creation. Extra particularly, Python supplies a number of strategies and features used to search out and initialize the dictionary, making it usable for different information constructions in these strategies.

This write-up will talk about the a number of methods for the initialization of a dictionary in Python.

Initialize a Dictionary in Python?

There are a number of strategies out there in Python which can be used to initialize a dictionary. A few of them are listed beneath:

Methodology 1: Initialize a Dictionary in Python Utilizing Dictionary Comprehension

The dictionary comprehension is the simplest technique to initialize a dictionary in Python that’s fairly much like the listing comprehension.

Instance

To initialize the dictionary, use the “for” loop that takes the vary by using the “vary()” technique:

my_dict = {my_list: [] for my_list in vary(3)}

Print the worth of initialized dictionary worth:

print (“My New Initialized Dictionary: “ + str(my_dict))

Output

Methodology 2: Initialize a Dictionary in Python Utilizing “{}” Braces Methodology

One other best technique to initialize the dictionary in Python is utilizing the curly braces “{}” technique.

Instance

To initialize the empty dictionary, use the “{}” braces:

Apply the print assertion to the “my_dic” variable to get its worth:

print(“My New Empty Initialized Dictionary: “, my_dic)

As you possibly can see, the empty listing has been initialized efficiently:

Methodology 3: Initialize a Dictionary in Python Utilizing “fromkey()” Methodology

Use the “fromkey()” technique to create and initialize a dictionary from the required keys sequence and values.

Instance

First, declare a listing that incorporates three strings:

keyList = [“Linux”, “Hint”, “World”]

Specify the quantity to which we need to initialize with the offered listing strings:

Name the “fromkeys()” technique on the generated listing to transform the strings into keys and shops them to the “my_dic” variable:

my_dic = dict.fromkeys(keyList, num)

Use the “print()” perform to get the initialized dictionary:

print(“My New Initialized Dictionary: “ , my_dic)

Output

Methodology 4: Initialize a Dictionary in Python Utilizing “defaultdict()” Methodology

The “defaultdict()” technique will be utilized to initialize a dictionary in Python. It’s a easy method to make use of any key with out initialization of worth.

Instance

Import the “defaultdict” module from the “collections” library:

from collections import defaultdict

Use the “defaultdict()” technique with the listing as an argument for initializing the dictionary and save them to the “my_dict” variable:

my_dict = defaultdict(listing)

Carry out the append operation by using the “append()” technique and specify the string:

my_dict[0].append(‘LinuxHint’)

Use the “dict()” technique to get the dictionary contained in the print assertion:

print (“My New Initialized Dictionary: “ + str(dict(my_dict)))

Output

Methodology 5: Initialize a Dictionary in Python Utilizing “setdefault()” Methodology

To initialize a dictionary in Python, the “setdefault()” technique can be utilized by specifying the key-value pairs throughout the comprehension.

Instance

Initialize the dictionary with an empty listing:

Invoke the “setdefault()” perform with arguments with the “for” loop and specify the vary by using the “vary()” perform:

[my_dict.setdefault(x, []) for x in vary(3)]

Make the most of the “append()” perform with the string as argument:

my_dict[0].append(‘LinuxHint’)

Name the “dict()” perform throughout the print assertion to get the initialized dictionary:

print (“My New Initialized Dictionary: “ + str(dict(my_dict)))

It may be noticed that the dictionary has been initialized efficiently:

Methodology 6: Initialize a Dictionary in Python Utilizing “dict()” and “zip()” Strategies

The “dict()” and “zip()” strategies are additionally used for initializing a dictionary in Python. Each are built-in features which can be utilized with the listing comprehension to attain the specified consequence.

Instance

Use the “vary()” technique by specifying the specified vary and storing into the “dic_key

Use the “zip()” technique with required arguments and offered vary with “for” loop contained in the “dict()” technique:

my_dict = dict(zip(dic_keys, ([] for _ in dic_keys)))

Now, append via the “append()” technique with listing index:

my_dict[0].append(‘LinuxHint’)

Name the “print()” perform:

print(“My New Initialized Dictionary: “ + str(dict(my_dict))

Output

Methodology 7: Initialize a Dictionary in Python By Passing Parameters

We will additionally initialize the dictionary by passing arguments utilizing the “dict()” built-in technique.

Instance

Use the “dict()” technique and go the values within the parameters to a dictionary:

my_dic = dict(Linux=0, Trace=1, World=2)

Now, get the dictionary utilizing the print assertion:

print(“My New Initialized Dictionary: “, my_dic)

Output

That’s all! We now have offered a number of strategies to initialize a dictionary in Python.

Conclusion

To initialize the dictionary, the “fromkey()”, “defaultdict()”, “setdefault()”, “dict()”, “zip()” strategies, passing arguments, curley “{}” braces, and the dictionary comprehension strategies are used. This write-up demonstrated a number of strategies to initialize a dictionary in Python.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments