Would you like to react to this message? Create an account in a few clicks or log in to continue.

You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1Python dictionary data type Empty Python dictionary data type Fri Sep 24, 2010 2:57 am

Erebus

Erebus
Achiever
Loading
I did not write most of this i took different parts from different sites Razz
But stil +rep for bring it all together for you guys

A dictionary is mutable and is another container type that can store any number of Python objects, including other container types.

Dictionaries consist of pairs (called items) of keys and their corresponding values.

Python dictionaries are also known as associative arrays or hash tables. The general syntax of a dictionary is as follows:
Code:

dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}

You can create dictionary in the following way as well:

Code:
dict1 = { 'abc': 456 };
dict2 = { 'abc': 123, 98.6: 37 };

Each key is separated from its value by a colon (Smile, the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}.

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.
Accessing Values in Dictionary:

To access dictionary elements, you use the familiar square brackets along with the key to obtain its value:
Example:

Code:
#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};

print "dict['Name']: ", dict['Name'];
print "dict['Age']: ", dict['Age'];

This will produce following result:

dict['Name']:  Zara
dict['Age']:  7

If we attempt to access a data item with a key which is not part of the dictionary, we get an error as follows:

Code:
#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};

print "dict['Alice']: ", dict['Alice'];

This will produce following result:

dict['Zara']:
Traceback (most recent call last):
  File "test.py", line 4, in <module>
    print "dict['Alice']: ", dict['Alice'];
KeyError: 'Alice'
Updating Dictionary:

You can update a dictionary by adding a new entry or item (i.e., a key-value pair), modifying an existing entry, or deleting an existing entry as shown below:
Example:

Code:
#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};

dict['Age'] = 8; # update existing entry
dict['School'] = "DPS School"; # Add new entry


print "dict['Age']: ", dict['Age'];
print "dict['School']: ", dict['School'];

This will produce following result:

dict['Age']:  8
dict['School']:  DPS School

Delete Dictionary Elements:

You can either remove individual dictionary elements or clear the entire contents of a dictionary. You can also delete entire dictionary in a single operation.

To explicitly remove an entire dictionary, just use the del statement:
Example:
Code:

#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};

del dict['Name']; # remove entry with key 'Name'
dict.clear();    # remove all entries in dict
del dict ;        # delete entire dictionary

print "dict['Age']: ", dict['Age'];
print "dict['School']: ", dict['School'];

This will produce following result. Note an exception raised, this is because after del dict dictionary does not exist any more:

Code:
dict['Age']:
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    print "dict['Age']: ", dict['Age'];
TypeError: 'type' object is unsubscriptable

Note: del() method is discussed in subsequent section.
Properties of Dictionary Keys:

Dictionary values have no restrictions. They can be any arbitrary Python object, either standard objects or user-defined objects. However, same is not true for the keys.

There are two important points to remember about dictionary keys:

(a) More than one entry per key not allowed. Which means no duplicate key is allowed. When duplicate keys encountered during assignment, the last assignment wins.
Example:

Code:
#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'};

print "dict['Name']: ", dict['Name'];

This will produce following result:

dict['Name']:  Manni

(b) Keys must be immutable. Which means you can use strings, numbers, or tuples as dictionary keys but something like ['key'] is not allowed.
Example:
Code:

#!/usr/bin/python

dict = {['Name']: 'Zara', 'Age': 7};

print "dict['Name']: ", dict['Name'];

This will produce following result. Note an exception raised:
Code:

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    dict = {['Name']: 'Zara', 'Age': 7};
TypeError: list objects are unhashable

2Python dictionary data type Empty Re: Python dictionary data type Tue Dec 21, 2010 10:47 pm

The Professor

The Professor
Contributor
Loading
The result it produces is invalid.
Rendering this thread null.

3Python dictionary data type Empty Re: Python dictionary data type Sat Feb 12, 2011 10:48 pm

Erebus

Erebus
Achiever
Loading
Proto you fail troll .-.

4Python dictionary data type Empty Re: Python dictionary data type Sat Feb 12, 2011 11:15 pm

The Professor

The Professor
Contributor
Loading
TzukiX wrote:Proto you fail troll .-.

TzukiX you fail idiot .-.
But you do realize what I said was true, right?
Instead of copying & pasting from other websites you should of actually tested the code.

5Python dictionary data type Empty Re: Python dictionary data type Tue Feb 15, 2011 1:11 am

Erebus

Erebus
Achiever
Loading
Protozoid wrote:
TzukiX wrote:Proto you fail troll .-.

TzukiX you fail idiot .-.
But you do realize what I said was true, right?
Instead of copying & pasting from other websites you should of actually tested the code.
Hmm I dont use python i did for two days. i am posting codes for the users i do . i dont needa test them because i dont want to.
And Yes what you saaid was true but your still trying to troll me.

6Python dictionary data type Empty Re: Python dictionary data type Tue Feb 15, 2011 1:13 am

The Professor

The Professor
Contributor
Loading
TzukiX wrote:
Protozoid wrote:
TzukiX wrote:Proto you fail troll .-.

TzukiX you fail idiot .-.
But you do realize what I said was true, right?
Instead of copying & pasting from other websites you should of actually tested the code.
Hmm I dont use python i did for two days. i am posting codes for the users i do . i dont needa test them because i dont want to.
And Yes what you saaid was true but your still trying to troll me.

Nice assumption. When someone posts something that you don't like, just avoid assuming that they're trying to troll. I had no intention of trolling you at all.

7Python dictionary data type Empty Re: Python dictionary data type Tue Feb 15, 2011 11:28 pm

Rouge

Rouge
Achiever
Loading
Tz you fail

8Python dictionary data type Empty Re: Python dictionary data type Thu Apr 07, 2011 11:36 pm

Erebus

Erebus
Achiever
Loading
Joker wrote:Tz you fail
Fag you jelous bro? Cool

9Python dictionary data type Empty Re: Python dictionary data type Fri Apr 08, 2011 4:28 am

des

des
Achiever
Loading
TzukiX wrote:
Joker wrote:Tz you fail
Fag you jelous bro? Python dictionary data type 55184
jealous* And why would he be 'jealous'? You Leak all the programs you like, proof
Python dictionary data type E5b3d6296a50c26335e176eb18
Im back and better then ever- Leaking alot of programs Python dictionary data type 297612
You should start to make your own shit up, don't take, just make. -rep.

10Python dictionary data type Empty Re: Python dictionary data type Fri Apr 08, 2011 11:15 am

Rouge

Rouge
Achiever
Loading
Joker wrote:Tz you fail
WTF I don't recall posting that.... wtf??? Python dictionary data type 420510

11Python dictionary data type Empty Re: Python dictionary data type Fri Apr 08, 2011 8:06 pm

Erebus

Erebus
Achiever
Loading
Desu gtfo ^^

12Python dictionary data type Empty Re: Python dictionary data type Fri Apr 08, 2011 10:12 pm

des

des
Achiever
Loading
There's really nothing that can help you. It's better for you to leave and tell Raze to delete this thread.

13Python dictionary data type Empty Re: Python dictionary data type Sat Apr 09, 2011 6:02 pm

Erebus

Erebus
Achiever
Loading
Id rather it stay so every1 can see how much of a fail at life you are.

14Python dictionary data type Empty Re: Python dictionary data type Sat Apr 09, 2011 6:28 pm

des

des
Achiever
Loading
Python dictionary data type 2cpdu94
>You fail
>Everyone hates you
>You can't program
>You leak from others
>You have no life

Now go make everyone happy. Go die.

15Python dictionary data type Empty Re: Python dictionary data type Mon Apr 11, 2011 11:37 pm

Erebus

Erebus
Achiever
Loading
> I dont really "Fail" Because i cant fail, i dont do anything to fail at Wink
>Alot of people like me unlike your troll ass.
>I dont program anymore so try again
> Please tell me what have i leaked again?
> You spend you whole life on myspace and on programming websites, i cant program bcuz i dont waste my time unlike ur ass
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
But nice try, Try once mor if ud like:)

16Python dictionary data type Empty Re: Python dictionary data type Tue Apr 12, 2011 12:11 am

des

des
Achiever
Loading
Spend my life on Mysapce, programming? Son, you need to learn a little something from me now.

Hello, I'm zirDesu, the sites Designer. I don't program anymore, and I don't like you. I spend my life with my girlfriend and using Photoshop. I don't do HTML, CSS, xHTML anymore. People do like me on this site.

Before you even type a come back or anything. Make sure you read it, read it again, post it, and wait 'till I post back so you can rage and make a big post and feel proud of yourself.

Do I feel proud of myself? Yes indeed, why? You're reading this.
Embarassed

Last thing, have a nice day ya hear?

17Python dictionary data type Empty Re: Python dictionary data type Tue Apr 12, 2011 12:17 pm

Thomas

Thomas
Admin
Loading
Forum Locked -Gravedigging

Sponsored content


Loading

View previous topic View next topic Back to top  Message [Page 1 of 1]

Related topics

Permissions in this forum:
You cannot reply to topics in this forum