Python Day2 data types: List tuples and dictionaries

Source: Internet
Author: User

List (List)

A list is a set of ordered sets of data that can be stored in a list in an orderly manner, and can be added to and removed from, and traversed. The list exists in order to store more information through a variable.


through Len () built-in functions to see the number of elements in a list

>>> len (list)

you can find the location of each element in the list by index, remembering that the index is from 0 started with

>>> shopping_list[2] # Find Bike

>>> Shopping_list[0] # The first element is taken out of
>>> Shopping_list[-1] #-1 represents the last element in the list ' Gift '
>>> shopping_list[-3] # take the penultimate 3 -bit element ' Clothes '
>>> shopping_list[-4] # take the bottom 4 elements

slices (Slice)


You can also remove a specified number of elements from the list, which is called slicing


>>> Shopping_list[0:3] #Take0to the first3elements, not including the first4a
>>> Shopping_list[:3] #Ibid., Take0to the first3elements, not including the first4A,0can not write
>>> Shopping_list[2:5] #Take section3to section5an element
>>> Shopping_list[:-3] #take from0to the bottom of the page3an element
>>> shopping_list[-3:] #take the last3an element
>>> Shopping_list[1:8:2] #from1to8take one from the other, the back one.2is the step, that is, every few elements take a
>>> Shopping_list[::2] #take one from each other in place

Change and delete

>>> shopping_list.append (' Movieticket ') #append an element to the back of the list
>>> Shopping_list.pop () #Delete last element
>>> shopping_list.remove (' Mac ') #Delete called' Mac 'the element, if there are multiple' Mac ',that will delete the first one found from the left number
>>> shopping_list[2] = ' COFFEE ' #the index is2the element is changed to"COFFEE", it turns out to be lowercase
>>> Shopping_list.insert (3, "Toy") #inserts a new element, indexed as3

>>> shopping_list.index (' Toy ') #return' Toy 'The index value of the element, if there are multiple identical elements, returns the first matching
>>> shopping_list.append (' food ')
>>> shopping_list.count (' food ') #Statistics' food 'the number of elements, just added one, so now is2a
>>> list2= [' Banana ', ' Apple '] #Create a new list
>>> Shopping_list.extend (list2) #Merge the new list above into theshopping_listin
>>> Shopping_list.sort () #sort the list
>>> Shopping_list.reverse () #Invert the list
>>> del Shopping_list[3:8] #Delete Index3to8the element, not including8
>>> for I in Shopping_list: #Traverse List
... print I







This article from the "Wind continue to blow" blog, declined reprint!

Python Day2 data types: List tuples and dictionaries

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.