Sam Heather
Sam Heather

Reputation: 1503

Whats the difference in a Python Dictionary and a Data Structure?

Just studying data structures in python for a project and can't work out the difference in a dictionary and a data structures in this form:

class Address:

    def __init__(self, house_number, house_street, house_city,
                 house_county, house_postcode):
        self.number = house_number
        self.street = house_street
        self.city = house_city
        self.county = house_county
        self.postcode = house_postcode

    def __repr__(self):
        return ('<Class: \'Address\', '+ str(self.number) + ' ' +
                self.street + ' ' + self.city + ' ' +
                self.county + ' ' + self.postcode + '>')


## Creates an object Address and assign it to a variable
my_address = Address(1, 'abc', 'def', 'ghi', 'jkl')

## Print the object My_address
print 'Address object:', my_address

print 'house number:', my_address.number

## change house number
my_address.number = 555

print 'NEW house number:', my_address.number

The code sample is modified from sample code given in a lecture at university of york.

Upvotes: 2

Views: 444

Answers (2)

jkalivas
jkalivas

Reputation: 1163

Data structures in python can have the form of lists,dictionaries,even for emtpy classes that just use the pass keyword.They are like the struct in C. They are those objects that can actually store some data in some order or not.

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1122372

A python dict is a highly optimized hash table, which is a data structure.

So, to put it differently, a python dict is one example of a data structure. So are the set, list, string and unicode types. A custom class is again another data structure, one that is highly customizable to your application needs.

From the WikiPedia entry on Data structure:

In computer science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

Upvotes: 4

Related Questions