3 types of built-in data structures in Python: Lists, tuples, and dictionaries _python

Source: Internet
Author: User
Tags data structures in python

There are 3 types of built-in data structures in Python: Lists, tuples, and dictionaries. Refer to the Concise Python tutorial


1. List
A list is a data structure that handles a set of ordered items, that is, you can store a sequence of items in a list. Imagine you have a shopping list that says what you want to buy and you can easily understand the list. It's just that on your shopping list, you probably have everything on your own, and in Python, you separate each item with a comma.

The items in the list should be included in square brackets so that Python knows that you are specifying a list. Once you have created a list, you can add, delete, or search for items in the list. Since you can add or delete items, we say that the list is a mutable data type, that is, this type can be changed.
Example:

#!/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 would sort my list now '
list.sort ()
print ' Sorted list is%s '% list
 
print ' \nthe ', list[0]
item0 = list[0]
print ' delete ' >del list[0]
print ' list is now ', list

Output

$python using_list.py These
items are:linux Nginx MySQL PHP
add Apache.
List is now [' Linux ', ' Nginx ', ' MySQL ', ' PHP ', ' Apache ']
 
I'll sort my list now
Sorted list is [' Apache ', ' Linux ' , ' MySQL ', ' Nginx ', ' PHP '] the ' the ' of the ' the ', ' the ' the ' the ', '
P ']

2. META Group
Tuples and lists are very similar, except that tuples and strings are immutable , that is, you cannot modify tuples. Tuples are defined by a comma-separated list of items through parentheses. Tuples are typically used when a statement or user-defined function can safely adopt a set of values, that is, the value of the tuple being used does not change.
Example:

#!/usr/bin/env python
#coding: UTF8
 
zoo = (' Wolf ', ' Elephant ', ' penguin ')
print ' Number of animals in the Z Oo is ', Len (zoo)
 
New_zoo = (' monkey ', ' Dolphin ', zoo)
print ' Number of animals in the new Zoo is ', Len (New_zoo) C18/>print ' All animals in new zoo are ', New_zoo
print ' animals brought to old zoo are ', new_zoo[2]
print ' Las T animal brought from Old Zoo is ', new_zoo[2][2]

Output

$ python using_tuple.py number of animals in the zoo was 3 number of animals in the
New Zoo are 3 all
animals in New zoo are (' monkey ', ' Dolphin ', (' Wolf ', ' Elephant ', ' penguin ')) Animals brought from Old
Zoo are (' wolf ', ' Elephan T ', ' Penguin ') last
animal brought from Old Zoo is Penguin

3. Dictionary
The dictionary is similar to the address book where you look up the address and contact details by your contact name, that is, we associate the key (first name) with the value (details). Note that the key must be unique, just as 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 keys to a dictionary, but you can have immutable or mutable objects as the values of the dictionaries. Basically, you should just use a simple object as a key.

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

Remember that the key/value pairs in the dictionary are not in order. If you want a specific order, then you should sort them yourself before using them.

A dictionary is an instance/object of the Dict class.
Example:

#!/usr/bin/env python
#coding: UTF8
 
contacts = {' Admin ': ' admin@jb51.net ',
' linuxeye ': ' Linuxeye@jb51.net ',
' Support ': ' Support@jb51.net '
}
 
print ' Linuxeye ' 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-book\n '% len (contacts)
for name, address in C Ontacts.items ():
print ' contact%s ' in%s '% (name, address)
 
if Contacts.has_key (' Test '):
print "\ntest ' s Address '%s '% contacts[' test '

Output

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.