--> Dictionary- Python- Alpha Codist | Alpha Codist

Search This Blog

Dictionary- Python- Alpha Codist

Dictionary, What, Create a dictionary, Example, Merge dictionaries, kwargs, multiple dictionaries, with example, coding, make game, pygame, course

 Dictionary
Dictionary, What, Create a dictionary, Example, Merge dictionaries, kwargs, multiple dictionaries,  with example, coding, make game, pygame,  course

A dictionary is a collection that is unordered, changeable, and indexed. In Python, dictionaries are written with curly brackets{}, and they have keys and values.

Create a dictionary:

In Python, a Dictionary can be created by placing a sequence of elements within the curly brackets, separated by 'comma'. Dictionary holds a pair of values, one being the Key and the other corresponding pair element being its like 'Key: value'.

Example:



alpha = {
    'nadim': 5,
    'niyaz': 9,
    'nila':4
    }

codist = {
    'tim': 8,
    'rahim':4,
    'nadim':9
    }


Take or read data from the dictionary:

Let's print the value of a single key from the dictionary above.

Example:



alpha = {
    'nadim': 5,
    'niyaz': 9,
    'nila':4
    }

codist = {
    'tim': 8,
    'rahim':4,
    'nadim':9
    }

Data = alpha['nadim']

print(Data)

The result of this code is the value of the key nadim which is 5. You can even change the value of a key this way try it.

Merge dictionaries:

In Python, the Dictionary class provides a function update() i.e. 'dictionatyname.update(seconddictionaryname)' It accepts another dictionary or an Iterable object (collection of key-value pairs) as an argument. Then merges the contents of this passed dictionary or Iterable in the current dictionary.

Example:



alpha = {
    'nadim': 5,
    'niyaz': 9,
    'nila':4
    }

codist = {
    'tim': 8,
    'rahim':4,
    'nadim':9
    }

alpha.update(codist)

print(alpha)


Result:

Dictionary, What, Create a dictionary, Example, Merge dictionaries, kwargs, multiple dictionaries,  with example, coding, make game, pygame,  course

Using **kwargs we can send variable length key-value pairs to a function. When we apply '**' to a dictionary, then it expands the contents in the dictionary as a collection of key-value pairs

as an example, if we have a dictionary:



alpha = {
    'nadim': 5,
    'niyaz': 9,
    'nila':4
    }

codist = {
    'tim': 8,
    'rahim':4,
    'nadim':9
    }

best = {**alpha, **codist}

print(best)


Result:

Dictionary, What, Create a dictionary, Example, Merge dictionaries, kwargs, multiple dictionaries,  with example, coding, make game, pygame,  course

You can merge multiple dictionaries using kwargs.

Example:



alpha = {
    'nadim': 5,
    'niyaz': 9,
    'nila':4
    }

codist = {
    'tim': 8,
    'rahim':4,
    'nadim':9
    }

best = {
    'jerry':7,
    'bob': 71,
    'rahim':58
        }
site = {**alpha, **codist, **best}

print(site)


Result:

Dictionary, What, Create a dictionary, Example, Merge dictionaries, kwargs, multiple dictionaries,  with example, coding, make game, pygame,  course


Some of the data were collected. Source: (tutorialspoint.com, onlyscience360.blogspot.com)

COMMENTS

Name

beeCrowd,11,C,10,Computer,9,Downloads,3,HTML,1,My Creation,2,Python,24,Python-Soft,1,Shooting,2,
ltr
item
Alpha Codist: Dictionary- Python- Alpha Codist
Dictionary- Python- Alpha Codist
Dictionary, What, Create a dictionary, Example, Merge dictionaries, kwargs, multiple dictionaries, with example, coding, make game, pygame, course
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwVnUJeOOWk3QWDGDHCETjtnRUdj974kU25dV0ATguDjWLCM-2pPPVpPKTXbIob0YzKt6mMceyeeKGCHKj5UQp3RRK-ArL1V2WR_xrxF21HMIZgh_IaCbQZTJqlxTy3x8pA2I13zqXc6g7/w640-h422/Dictionary-01.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwVnUJeOOWk3QWDGDHCETjtnRUdj974kU25dV0ATguDjWLCM-2pPPVpPKTXbIob0YzKt6mMceyeeKGCHKj5UQp3RRK-ArL1V2WR_xrxF21HMIZgh_IaCbQZTJqlxTy3x8pA2I13zqXc6g7/s72-w640-c-h422/Dictionary-01.png
Alpha Codist
https://alphacodist.blogspot.com/2020/08/dictionary.html
https://alphacodist.blogspot.com/
https://alphacodist.blogspot.com/
https://alphacodist.blogspot.com/2020/08/dictionary.html
true
7547834254111195316
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content