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

Source: Internet
Author: User
There are 3 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. Just on your shopping list, it's possible that everything is on its own, and in Python, you're separating each item with a comma.

The items in the list should be enclosed 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.
Cases:

#!/usr/bin/env Python#coding:utf8 list = [' Linux ', ' Nginx ', ' MySQL ', ' PHP '] print ' These items is: ', for item in List:prin T item, print ' \nadd Apache. ' List.append (' Apache ') print ' list is now ', List print ' \ni'll sort my list now ' List.sort () pr int ' 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

Output

$python using_list.pythese items are:linux Nginx mysql phpadd apache.list are now [' Linux ', ' Nginx ', ' MySQL ', ' PHP ', ' Apac He '] I'll sort my list nowsorted list is [' Apache ', ' Linux ', ' MySQL ', ' Nginx ', ' PHP '] The first item Apachedelete first Itemlist is now [' Linux ', ' MySQL ', ' Nginx ', ' PHP ']

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

#!/usr/bin/env Python#coding:utf8 Zoo = (' Wolf ', ' Elephant ', ' penguin ') print ' number of animals in the zoo is ', Len (ZOO) n Ew_zoo = (' monkey ', ' Dolphin ', zoo) print ' Number of animals in the new zoo was ', Len (new_zoo) print ' All animals in new zoo Was ', New_zooprint ' Animals brought from the old Zoo was ', New_zoo[2]print ' last animal brought from the old Zoo is ', new_zoo[2][2 ]

Output

$ python using_tuple.pynumber of animals in the zoo was 3Number of animals in the new zoo was 3All animals in New zoo was (' Monkey ', ' Dolphin ', (' Wolf ', ' Elephant ', ' penguin ')) Animals brought from the old Zoo is (' Wolf ', ' Elephant ', ' penguin ') last A Nimal brought from the old Zoo is Penguin

3. Dictionaries
The dictionary is similar to the Address book where you find the address and contact details by contact name, that is, we associate the key (first name) with the value (details). Note that the key must be unique, as if two people happen to have the same name, you cannot find the correct information.

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

Key-value pairs are tagged 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 is separated by commas, all of which are included in curly braces.

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

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

#!/usr/bin/env Python#coding:utf8 contacts = {' Admin ': ' admin@jb51.net ', ' linuxeye ': ' linuxeye@jb51.net ', ' Support ': ' s Upport@jb51.net '} print "Linuxeye's address is%s"% contacts[' Linuxeye '] # Adding a key/value paircontacts[' test '] = ' tes T@jb51.net ' # Deleting a key/value pairdel contacts[' support '] print ' \nthere is%d contacts in the address-book\n '% len (contacts) for name, address in Contacts.items ():p rint ' contact%s at%s '% (name, address) if Contacts.has_key (' Test '):p RI NT "\ntest s address is%s"% contacts[' test ']

Output

$ Python Using_dict.pylinuxeye's address is linuxeye@jb51.net there be 3 contacts in the Address-book contact Admin at ad Min@jb51.netcontact Test at Test@jb51.netcontact linuxeye at Linuxeye@jb51.net Test's address is test@jb51.net
  • 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.