list inside dictionary in python by R4R Team

- list contain only elements or items like number,string or other data typr as well.
- list work on the basis of the index value
- dictionary contain keys and value, where keys plays a role of index value.
- here key is unique value like numbers, string, not the list and value supports all type of data type.

So it is possible to insert a list at the place of values in dictionary......


program-

l=[1,2,3]
print("List is",l)
d={1:l,2:l,3:l,"one":l,"two":l}
print("Dictionary is n",d)

print(d[1])
print(d[2])
print(d[3])
print(d["one"])
print(d["two"])


output-

List is [1, 2, 3]
Dictionary is
{1: [1, 2, 3], 2: [1, 2, 3], 3: [1, 2, 3], 'one': [1, 2, 3], 'two': [1, 2, 3]}
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]


-In this program, we have a list [1,2,3,4], so we pass in dictionary as a value for each and every keys of dictionary.

-If we try to put list at the place of keys like


program-

l=[1,2,3]
d={l:l}
print(d)


output-

d={l:l}
TypeError: unhashable type: 'list'


-That means we can't put the list at the place of keys in dictionary in python.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!