blog posts

Dictionary training in Python and review its methods – Dictionary in Python and methods used

Dictionary training in Python is one of the most popular programming languages. Python is a general-purpose programming language, and many websites and software known for writing it. 

Dictionary training in Python is a powerful language with many capabilities and features; One of these features is the dictionary; You must have used a dictionary and are familiar with how to work with them.

Introduction to dictionary training in Python

 

In the dictionary tutorial in python, you will find that working with dictionaries is very simple, you just need to know what you are looking for, by having the desired word in the dictionary, you will look for that word, and by finding that word, you can read all its descriptions. . Dictionaries do not repeat a keyword more than once, but there are no restrictions on its description, and sometimes the description reaches several lines. Many programming languages ​​are similar to human languages ​​with dictionaries.

Dictionaries in Python are similar to real-world dictionaries and follow their principles. To better learn this language, you can refer to the article What is Python Programming Language? See.

Python dictionaries are defined by two key elements and a value. In fact, the keys are the same words and the same description values ​​in dictionaries. Keys must consist of only one element; We will talk about them later.

Dictionary concept in Python

We learn about accessing and managing dictionaries. In this tutorial, we have tried to review all the concepts of dictionaries. A dictionary in Python is another type of composite data that is very similar to lists. Dictionaries are similar to associative arrays in languages ​​like Java and php.

The order in the lists is important and the elements are stored in a certain order, the indexes of the elements are numeric and the first element in the list and the tuple is zero and the second element is index 1 and so on; In the Python dictionary, the type of access to dictionary values ​​differs from lists and tops;

Dictionaries in Python and lists have many similarities:

  • Both are changeable.
  • Both are dynamic and can be expanded or shortened if needed.
  • And Both can be nested. A list can contain another list, and a dictionary in Python can contain another dictionary. Also, a dictionary in Python can contain a list and vice versa.

Dictionaries Python

Dictionaries can be any type of data, but they are often numeric or string data, in the Python Dictionary we use more numeric and string data. The  dictionaries are defined in parentheses or {and, and parentheses or operators [] must be used to access its values. A dictionary is actually a collection of related data pairs. The information contained in dictionaries is usually related to each other.

 

Knowledge of dictionary training in Python

 

But dictionaries differ from lists in the type of access to elements. In the lists, the elements are arranged in an orderly manner, and access to them is possible depending on their location in the list. This dictionary tutorial in Python will teach you how to define and access dictionary values ​​completely.

Dictionary definition in Python

Implementing a dictionary in Python is similar to defining an array. Dictionaries are generally a collection of key-value pairs. You should also keep in mind that a key can have more values, meaning we can assign more descriptions to a key.

1. You can define a dictionary inside a pair of supporting symbols (}.). Each element in the dictionary is separated by a comma from the next element, and each element is a key: value pair, in which the key acts as an index and the value as the value assigned to the index, one or two points separating each key from its value:

2. You can also define the function of pre-defined dictionary of Python “(dict)” use in the training dictionary in Python than the first method is used.

The argument inside this function must be a key-value pair: Once you have defined a dictionary, you can display its contents whenever you want.

Display dictionary content in Python

One of the differences between a Python dictionary and real dictionaries is the lack of order in these dictionaries. At the time of display, all dictionary contents are displayed in the order in which they were defined. But this display does not apply to dictionaries, because dictionary elements with numeric indexes are not accessible.

Access to dictionary content

You can view all the contents of a dictionary at once or view each value individually. You can also create a cycle on dictionary content. Dictionary content is accessed by keys. One of the easiest ways to access dictionary content is to access the element by inserting a key inside the ([]) symbol:

 

How to teach a dictionary in Python

 

Surely you are familiar with the function (print) in Python, you can simply see the value by placing the key inside the function opposite (print (team [‘perspolis’]), you can also put the dictionary name in The print function displays all the content in the dictionary, but you can only know the content in the dictionary; then you will learn how you can change this content or what kind of changes you can make to this content.

As mentioned in this dictionary tutorial in Python , dictionaries are dynamic, meaning you can update them whenever you want (add new key-value, delete key-value, rewrite value ). Use the phrase “del dictionary name” to delete one of the values.

How to add and remove key-values ​​from the dictionary

* Although access to dictionary values ​​is not related to their order, Python maintains the order as the key-values ​​are defined and displays them in the same way when displayed. Also, when you define a new key-value, new content is placed at the end of the dictionary, and when you remove a key-value from the dictionary, the order of the remaining values ​​is maintained in the same way.

In the dictionary definition, you can have a variety of content in a dictionary, such as: numbers, lists, strings or other dictionaries. The same is true of the definition of keys, just as there is no limit to the definition of private values.

 

How to teach a dictionary in Python

 

If you want, you can define an empty dictionary and put content in it whenever you want:

Content types in the dictionary

To access the list defined within a dictionary, we need the index of that list; We also need the dictionary key listed to access the dictionary values ​​defined within another dictionary:

Access to the contents of lists and dictionaries within a dictionary As mentioned, not all keys need to be the same type when selecting a key in the dictionary:

Types of keys in the dictionary

You can use dictionaries for a variety of purposes, but there are some restrictions on keys and values ​​that you should consider. Continuing education Python dictionary on the limitations of our review. You can also refer to the article Getting to Know Python Programming Language Better .

Dictionary Key Restrictions

By now you must have realized that data types can be selected for key names and it is not necessary that the key and its value have the same type, you can also define pre-made Python objects and functions as keys.

 

But there are limitations in choosing the keys:

  1. Each key can be defined only once. Python does not accept duplicate keys. The dictionary assigns each key to a specific value, so it is wrong to use one for different values. If you redefine another value for a key after defining one key, the contents of the key will be overwritten and the original value will be lost.
  2. Selected keys must be immutable, such as: int, string, Boolean. Regular pairs can also be used as a key. But list and dictionary can not be considered as keys because both are variable.
  3. A dictionary in Python is case sensitive; This means that a key can be written once in lower case and once in uppercase, and the Python Dictionary treats each as a separate key.
  4. As you can see, there are no restrictions on the content of a dictionary, and all types of data, even lists and dictionaries, can be defined as the content of a dictionary.

Pre-built Python methods

Similar to lists, there are several pre-built methods in Python that can be implemented for dictionaries, which we will discuss below:

  •  (Dictionaryname.clear)
  • Clears all dictionary content, ie all key-values, and empties the dictionary.
  • (‘Dictionaryname.get key’)
  • The Python (get) method is a simple way to access the content we are looking for. In fact, this method searches for the named key, regardless of whether it exists or not, and returns the result:
  • Example of using .clear), .get)

 

Introducing Dictionary Training in Python

Dictionary training in Python Dictionaryname.items

This method returns a list of key-value pairs as a result. The first item in each pair is the ordered key and the second item is the key value.

 Dictionaryname.keys

This method displays a list of all the keys in the dictionary (examples of .items, .keys) methods

Dictionaryname.values

This method is similar to the items method, except that (values) returns a list of values ​​in the dictionary and has nothing to do with their keys. Each element is displayed in the dictionary as many times as it is repeated.

Clears the key we want from the dictionary and displays its value.

 

You will see that when we execute this method, the key named “c” is in the dictionary, so this key is removed and its value is displayed. But when we give the “z” key to the method, we get an error message because this key does not exist in the dictionary.

 Dictionaryname.popitem

A dictionary is used randomly to delete a key-value pair. If an empty dictionary gives an error message.

 Dictionaryname.update

Merges a dictionary with another dictionary or key-value pair.
If obj is a dictionary:

  • If the key does not exist in our dictionary, the key will be added to our dictionary from within obj.
  • And If the key is in our dictionary, its value changes to the same key value as obj.

As you can see in the example above, in merging the two dictionaries f, g, the two keys “q” and “o” have been added to the f dictionary, and the two keys “a” and “m” in the f dictionary have been rewritten. There are two ways to merge one or more keys with a dictionary:

  1. Enter the key-value in even order
  2.  Enter them as a list of keys with their values

 

Definition of dictionary training in Python

Dictionaryname.setdefault

This method takes a key and a value as input and looks for the key mentioned in our dictionary, if the key had a value, it returns that value, otherwise the value given to the method Was given as the content of that key.

This method is another way to access dictionary content. When using this method, mentioning the name of the key is mandatory, but entering the value is not mandatory.

 Dictionaryname.copy

Makes a copy of an existing dictionary. It has no input element. (Len) If you want, you can use this method to get the dictionary size. Example of method (len)

 Browse and display dictionary content (create a cycle on the dictionary)

If you are familiar with writing for in Python, this is very simple. All you have to do is merge the loop cycle and one of the methods mentioned above; In fact, you have to use one cycle for each of the key-values ​​to execute the method you want once.

We examined the basic concepts of the dictionary, such as how to access and make changes to dictionary information. Dictionaries in programming languages ​​are generally a type of data structure that allows you to store information that is somehow related to each other.

 

Introducing Dictionary Training in Python

 

Lists and dictionaries are the two most commonly used types of data in Python. As you can see, they have many similarities. But they differ in how they access the content.

Lists are indexed by key in specified order and dictionary elements.

That’s why lists and dictionaries are used for different uses. There are many reasons to use dictionaries, which we mentioned in this dictionary tutorial in Python , including:

  •  Information is stored in dictionaries without order and like a map.
  • A dictionary in Python is similar to a dictionary in the real world, and working with it requires knowing the keyword and is very simple.