Understanding Dictionary and Dictionary construct
I'm just started into the python learning and trying to understand the different construct of dictionary while going through dictionary reading i know it has key and Values format but i have few doubts to understand.
I have Below two dictionary called d1
and d2
>>> d1
{'A': 'one', 'B': 'two', 'C': 'three'}
>>> type(d1)
<class 'dict'>
In the above sample i see proper dictionary key and value format and while looking at type it says class dict
>>> d2
{'A': ['B', 'C']}
>>> type(d2)
<class 'dict'>
While the above sample i see dictionary key is okay but values are in list format and while looking at type it also says class dict
So, How could i process the d2
into a proper dictionary format key and value
. saying that i want to process d2
so it looks like as d1
then how could that be achieved.
Apologies if something i could not make clear.
python dictionary
add a comment |
I'm just started into the python learning and trying to understand the different construct of dictionary while going through dictionary reading i know it has key and Values format but i have few doubts to understand.
I have Below two dictionary called d1
and d2
>>> d1
{'A': 'one', 'B': 'two', 'C': 'three'}
>>> type(d1)
<class 'dict'>
In the above sample i see proper dictionary key and value format and while looking at type it says class dict
>>> d2
{'A': ['B', 'C']}
>>> type(d2)
<class 'dict'>
While the above sample i see dictionary key is okay but values are in list format and while looking at type it also says class dict
So, How could i process the d2
into a proper dictionary format key and value
. saying that i want to process d2
so it looks like as d1
then how could that be achieved.
Apologies if something i could not make clear.
python dictionary
They are both "proper" dictionaries. The type of a dictionary variable tells you nothing about the type of the values. If you'd like to see what the type of the values are, simply do something liketype(next(iter(d2.values())))
(or just index the dictionary with a key that you know). What do you want to get as output? I don't understand at all what you're asking to do.
– Alexander Reynolds
Nov 23 '18 at 7:11
@AlexanderReynolds, i tried that it does not work and saysTypeError: 'dict_values' object is not an iterator
– kulfi
Nov 23 '18 at 7:13
Yes, I edited the comment as I realized I left out part of it.
– Alexander Reynolds
Nov 23 '18 at 7:13
@AlexanderReynolds, thnx for edit, i have edited my question just to make it more claer as i could.
– kulfi
Nov 23 '18 at 7:16
add a comment |
I'm just started into the python learning and trying to understand the different construct of dictionary while going through dictionary reading i know it has key and Values format but i have few doubts to understand.
I have Below two dictionary called d1
and d2
>>> d1
{'A': 'one', 'B': 'two', 'C': 'three'}
>>> type(d1)
<class 'dict'>
In the above sample i see proper dictionary key and value format and while looking at type it says class dict
>>> d2
{'A': ['B', 'C']}
>>> type(d2)
<class 'dict'>
While the above sample i see dictionary key is okay but values are in list format and while looking at type it also says class dict
So, How could i process the d2
into a proper dictionary format key and value
. saying that i want to process d2
so it looks like as d1
then how could that be achieved.
Apologies if something i could not make clear.
python dictionary
I'm just started into the python learning and trying to understand the different construct of dictionary while going through dictionary reading i know it has key and Values format but i have few doubts to understand.
I have Below two dictionary called d1
and d2
>>> d1
{'A': 'one', 'B': 'two', 'C': 'three'}
>>> type(d1)
<class 'dict'>
In the above sample i see proper dictionary key and value format and while looking at type it says class dict
>>> d2
{'A': ['B', 'C']}
>>> type(d2)
<class 'dict'>
While the above sample i see dictionary key is okay but values are in list format and while looking at type it also says class dict
So, How could i process the d2
into a proper dictionary format key and value
. saying that i want to process d2
so it looks like as d1
then how could that be achieved.
Apologies if something i could not make clear.
python dictionary
python dictionary
edited Nov 23 '18 at 7:14
kulfi
asked Nov 23 '18 at 7:08
kulfikulfi
133
133
They are both "proper" dictionaries. The type of a dictionary variable tells you nothing about the type of the values. If you'd like to see what the type of the values are, simply do something liketype(next(iter(d2.values())))
(or just index the dictionary with a key that you know). What do you want to get as output? I don't understand at all what you're asking to do.
– Alexander Reynolds
Nov 23 '18 at 7:11
@AlexanderReynolds, i tried that it does not work and saysTypeError: 'dict_values' object is not an iterator
– kulfi
Nov 23 '18 at 7:13
Yes, I edited the comment as I realized I left out part of it.
– Alexander Reynolds
Nov 23 '18 at 7:13
@AlexanderReynolds, thnx for edit, i have edited my question just to make it more claer as i could.
– kulfi
Nov 23 '18 at 7:16
add a comment |
They are both "proper" dictionaries. The type of a dictionary variable tells you nothing about the type of the values. If you'd like to see what the type of the values are, simply do something liketype(next(iter(d2.values())))
(or just index the dictionary with a key that you know). What do you want to get as output? I don't understand at all what you're asking to do.
– Alexander Reynolds
Nov 23 '18 at 7:11
@AlexanderReynolds, i tried that it does not work and saysTypeError: 'dict_values' object is not an iterator
– kulfi
Nov 23 '18 at 7:13
Yes, I edited the comment as I realized I left out part of it.
– Alexander Reynolds
Nov 23 '18 at 7:13
@AlexanderReynolds, thnx for edit, i have edited my question just to make it more claer as i could.
– kulfi
Nov 23 '18 at 7:16
They are both "proper" dictionaries. The type of a dictionary variable tells you nothing about the type of the values. If you'd like to see what the type of the values are, simply do something like
type(next(iter(d2.values())))
(or just index the dictionary with a key that you know). What do you want to get as output? I don't understand at all what you're asking to do.– Alexander Reynolds
Nov 23 '18 at 7:11
They are both "proper" dictionaries. The type of a dictionary variable tells you nothing about the type of the values. If you'd like to see what the type of the values are, simply do something like
type(next(iter(d2.values())))
(or just index the dictionary with a key that you know). What do you want to get as output? I don't understand at all what you're asking to do.– Alexander Reynolds
Nov 23 '18 at 7:11
@AlexanderReynolds, i tried that it does not work and says
TypeError: 'dict_values' object is not an iterator
– kulfi
Nov 23 '18 at 7:13
@AlexanderReynolds, i tried that it does not work and says
TypeError: 'dict_values' object is not an iterator
– kulfi
Nov 23 '18 at 7:13
Yes, I edited the comment as I realized I left out part of it.
– Alexander Reynolds
Nov 23 '18 at 7:13
Yes, I edited the comment as I realized I left out part of it.
– Alexander Reynolds
Nov 23 '18 at 7:13
@AlexanderReynolds, thnx for edit, i have edited my question just to make it more claer as i could.
– kulfi
Nov 23 '18 at 7:16
@AlexanderReynolds, thnx for edit, i have edited my question just to make it more claer as i could.
– kulfi
Nov 23 '18 at 7:16
add a comment |
2 Answers
2
active
oldest
votes
In Python Dictionary are an unordered set of key: value pairs.
Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.
so you can store any type of object in value.
in first example you are storing string in value.
In second example you are storing list in value.
you can access the key value pair using below code.
d1 = {'A': 'one', 'B': 'two', 'C': 'three'}
d2 = {'A' : ['B', 'C'], 'D':['E','F']}
for k,v in d1.items():
print("key = {} value = {}".format(k,v))
for k,v in d2.items():
print("key = {} value = {}".format(k,v))
Output:
key = A value = one
key = B value = two
key = C value = three
key = A value = ['B', 'C']
key = D value = ['E', 'F']
Sach, thnx for your answer , can i get the d2 as{'A': 'B', 'A': 'C'}
– kulfi
Nov 23 '18 at 8:07
No, as I mentioned the key should be unique within dictionary so you can not have two value associated with same key.
– Sach
Nov 23 '18 at 8:12
But it can be converted reverse way like{Val:key for key, Value in d2.items() for Val in Value}
just i see
– kulfi
Nov 23 '18 at 8:51
add a comment |
In Python 3, you can just use .items() function
over the d2
dictionary as follows:
>>> for key, value in d2.items():
... print(f"{key} : {value}")
Try using the n2w python library to convert the number to words and flatten the dictionary to get the desired output.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442079%2funderstanding-dictionary-and-dictionary-construct%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In Python Dictionary are an unordered set of key: value pairs.
Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.
so you can store any type of object in value.
in first example you are storing string in value.
In second example you are storing list in value.
you can access the key value pair using below code.
d1 = {'A': 'one', 'B': 'two', 'C': 'three'}
d2 = {'A' : ['B', 'C'], 'D':['E','F']}
for k,v in d1.items():
print("key = {} value = {}".format(k,v))
for k,v in d2.items():
print("key = {} value = {}".format(k,v))
Output:
key = A value = one
key = B value = two
key = C value = three
key = A value = ['B', 'C']
key = D value = ['E', 'F']
Sach, thnx for your answer , can i get the d2 as{'A': 'B', 'A': 'C'}
– kulfi
Nov 23 '18 at 8:07
No, as I mentioned the key should be unique within dictionary so you can not have two value associated with same key.
– Sach
Nov 23 '18 at 8:12
But it can be converted reverse way like{Val:key for key, Value in d2.items() for Val in Value}
just i see
– kulfi
Nov 23 '18 at 8:51
add a comment |
In Python Dictionary are an unordered set of key: value pairs.
Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.
so you can store any type of object in value.
in first example you are storing string in value.
In second example you are storing list in value.
you can access the key value pair using below code.
d1 = {'A': 'one', 'B': 'two', 'C': 'three'}
d2 = {'A' : ['B', 'C'], 'D':['E','F']}
for k,v in d1.items():
print("key = {} value = {}".format(k,v))
for k,v in d2.items():
print("key = {} value = {}".format(k,v))
Output:
key = A value = one
key = B value = two
key = C value = three
key = A value = ['B', 'C']
key = D value = ['E', 'F']
Sach, thnx for your answer , can i get the d2 as{'A': 'B', 'A': 'C'}
– kulfi
Nov 23 '18 at 8:07
No, as I mentioned the key should be unique within dictionary so you can not have two value associated with same key.
– Sach
Nov 23 '18 at 8:12
But it can be converted reverse way like{Val:key for key, Value in d2.items() for Val in Value}
just i see
– kulfi
Nov 23 '18 at 8:51
add a comment |
In Python Dictionary are an unordered set of key: value pairs.
Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.
so you can store any type of object in value.
in first example you are storing string in value.
In second example you are storing list in value.
you can access the key value pair using below code.
d1 = {'A': 'one', 'B': 'two', 'C': 'three'}
d2 = {'A' : ['B', 'C'], 'D':['E','F']}
for k,v in d1.items():
print("key = {} value = {}".format(k,v))
for k,v in d2.items():
print("key = {} value = {}".format(k,v))
Output:
key = A value = one
key = B value = two
key = C value = three
key = A value = ['B', 'C']
key = D value = ['E', 'F']
In Python Dictionary are an unordered set of key: value pairs.
Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.
so you can store any type of object in value.
in first example you are storing string in value.
In second example you are storing list in value.
you can access the key value pair using below code.
d1 = {'A': 'one', 'B': 'two', 'C': 'three'}
d2 = {'A' : ['B', 'C'], 'D':['E','F']}
for k,v in d1.items():
print("key = {} value = {}".format(k,v))
for k,v in d2.items():
print("key = {} value = {}".format(k,v))
Output:
key = A value = one
key = B value = two
key = C value = three
key = A value = ['B', 'C']
key = D value = ['E', 'F']
edited Nov 28 '18 at 6:05
answered Nov 23 '18 at 7:39
SachSach
628417
628417
Sach, thnx for your answer , can i get the d2 as{'A': 'B', 'A': 'C'}
– kulfi
Nov 23 '18 at 8:07
No, as I mentioned the key should be unique within dictionary so you can not have two value associated with same key.
– Sach
Nov 23 '18 at 8:12
But it can be converted reverse way like{Val:key for key, Value in d2.items() for Val in Value}
just i see
– kulfi
Nov 23 '18 at 8:51
add a comment |
Sach, thnx for your answer , can i get the d2 as{'A': 'B', 'A': 'C'}
– kulfi
Nov 23 '18 at 8:07
No, as I mentioned the key should be unique within dictionary so you can not have two value associated with same key.
– Sach
Nov 23 '18 at 8:12
But it can be converted reverse way like{Val:key for key, Value in d2.items() for Val in Value}
just i see
– kulfi
Nov 23 '18 at 8:51
Sach, thnx for your answer , can i get the d2 as
{'A': 'B', 'A': 'C'}
– kulfi
Nov 23 '18 at 8:07
Sach, thnx for your answer , can i get the d2 as
{'A': 'B', 'A': 'C'}
– kulfi
Nov 23 '18 at 8:07
No, as I mentioned the key should be unique within dictionary so you can not have two value associated with same key.
– Sach
Nov 23 '18 at 8:12
No, as I mentioned the key should be unique within dictionary so you can not have two value associated with same key.
– Sach
Nov 23 '18 at 8:12
But it can be converted reverse way like
{Val:key for key, Value in d2.items() for Val in Value}
just i see– kulfi
Nov 23 '18 at 8:51
But it can be converted reverse way like
{Val:key for key, Value in d2.items() for Val in Value}
just i see– kulfi
Nov 23 '18 at 8:51
add a comment |
In Python 3, you can just use .items() function
over the d2
dictionary as follows:
>>> for key, value in d2.items():
... print(f"{key} : {value}")
Try using the n2w python library to convert the number to words and flatten the dictionary to get the desired output.
add a comment |
In Python 3, you can just use .items() function
over the d2
dictionary as follows:
>>> for key, value in d2.items():
... print(f"{key} : {value}")
Try using the n2w python library to convert the number to words and flatten the dictionary to get the desired output.
add a comment |
In Python 3, you can just use .items() function
over the d2
dictionary as follows:
>>> for key, value in d2.items():
... print(f"{key} : {value}")
Try using the n2w python library to convert the number to words and flatten the dictionary to get the desired output.
In Python 3, you can just use .items() function
over the d2
dictionary as follows:
>>> for key, value in d2.items():
... print(f"{key} : {value}")
Try using the n2w python library to convert the number to words and flatten the dictionary to get the desired output.
edited Nov 23 '18 at 7:28
answered Nov 23 '18 at 7:16
Kunal MukherjeeKunal Mukherjee
1,1791723
1,1791723
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442079%2funderstanding-dictionary-and-dictionary-construct%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
They are both "proper" dictionaries. The type of a dictionary variable tells you nothing about the type of the values. If you'd like to see what the type of the values are, simply do something like
type(next(iter(d2.values())))
(or just index the dictionary with a key that you know). What do you want to get as output? I don't understand at all what you're asking to do.– Alexander Reynolds
Nov 23 '18 at 7:11
@AlexanderReynolds, i tried that it does not work and says
TypeError: 'dict_values' object is not an iterator
– kulfi
Nov 23 '18 at 7:13
Yes, I edited the comment as I realized I left out part of it.
– Alexander Reynolds
Nov 23 '18 at 7:13
@AlexanderReynolds, thnx for edit, i have edited my question just to make it more claer as i could.
– kulfi
Nov 23 '18 at 7:16