There are three built in data structures in Python: lists tuples and dictionaries

  • 2020-04-02 14:26:53
  • OfStack

There are three built-in data structures in Python: lists, tuples, and dictionaries. Refer to the concise Python tutorial


List 1.
A list is a data structure that deals with a set of ordered items, that is, items that you can store a sequence of in a list. If you have a shopping list of things you want to buy, you can understand the list easily. It's just that on your shopping list, everything might have its own row, whereas in Python, you separate items by commas.

The items in the list should be included in square brackets so that Python knows you are specifying a list. Once you have created a list, you can add, remove, or search for items in the list. Since you can add or remove items, we say that lists are mutable data types, meaning that they can be changed.
Ex. :


#!/usr/bin/env python
#coding:utf8
 
list = ['Linux', 'Nginx', 'MySQL', 'PHP']
 
print 'These items are:',
for item in list:
print item,
 
print 'nadd Apache.'
list.append('Apache')
print 'list is now', list
 
print 'nI will sort my list now'
list.sort()
print 'Sorted list is %s' % list
 
print 'nThe first item ', list[0]
item0 = list[0]
print 'delete first item'
del list[0]
print 'list is now', list

The output


$python using_list.py
These items are: Linux Nginx MySQL PHP
add Apache.
list is now ['Linux', 'Nginx', 'MySQL', 'PHP', 'Apache']
 
I will sort my list now
Sorted list is ['Apache', 'Linux', 'MySQL', 'Nginx', 'PHP']
 
The first item Apache
delete first item
list is now ['Linux', 'MySQL', 'Nginx', 'PHP']

2. A tuple
Tuples are very similar to lists except that tuples are immutable like strings that you can't change. A tuple is defined as an item separated by a comma in parentheses. Tuples are typically used when statements or user-defined functions can safely adopt a set of values, that is, the values of the used tuples do not change.
Ex. :


#!/usr/bin/env python
#coding:utf8
 
zoo = ('wolf', 'elephant', 'penguin')
print 'Number of animals in the zoo is', len(zoo)
 
new_zoo = ('monkey', 'dolphin', zoo)
print 'Number of animals in the new zoo is', len(new_zoo)
print 'All animals in new zoo are', new_zoo
print 'Animals brought from old zoo are', new_zoo[2]
print 'Last animal brought from old zoo is', new_zoo[2][2]

The output


$ python using_tuple.py
Number of animals in the zoo is 3
Number of animals in the new zoo is 3
All animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))
Animals brought from old zoo are ('wolf', 'elephant', 'penguin')
Last animal brought from old zoo is penguin

3. The dictionary
A dictionary is similar to an address book where you look up addresses and contact details by the contact's name; that is, we associate keys (names) with values (details). Note that keys have to be unique, just like if two people happen to have the same name, you can't find the right information.

Note that you can only use immutable objects (such as strings) as dictionary keys, but you can use immutable or mutable objects as dictionary values. Basically, you should only use simple objects as keys.

Key-value pairs are marked in the dictionary in such a way that d = {key1: value1, key2: value2}. Note that their key/value pairs are separated by colons and each pair by commas, all of which are included in curly braces.

Remember that the key/value pairs in the dictionary are in no order. If you want a particular order, you should sort them yourself before you use them.

A dictionary is an instance/object of a dict class.
Ex. :


#!/usr/bin/env python
#coding:utf8
 
contacts = { 'Admin' : 'admin@jb51.net',
'Linuxeye' : 'linuxeye@jb51.net',
'Support' : 'support@jb51.net'
}
 
print "Linuxeye's address is %s" % contacts['Linuxeye']
 
# Adding a key/value pair
contacts['test'] = 'test@jb51.net'
 
# Deleting a key/value pair
del contacts['Support']
 
print 'nThere are %d contacts in the address-bookn' % len(contacts)
for name, address in contacts.items():
print 'Contact %s at %s' % (name, address)
 
if contacts.has_key('test'):
print "ntest's address is %s" % contacts['test']

The output


$ python using_dict.py
Linuxeye's address is linuxeye@jb51.net
 
There are 3 contacts in the address-book
 
Contact Admin at admin@jb51.net
Contact test at test@jb51.net
Contact Linuxeye at linuxeye@jb51.net
 
test's address is test@jb51.net


Related articles: