Python from entry to practice study notes (2) list

access

1. The index starts from 0

    The index is specified as -1, which tells Python to return the last list element.

    This convention also applies to other negative indices, for example, index -2 returns the second-to-last list element, index-3 returns the third-to-last list element, and so on.

Modify, add, delete elements

1. Modify

    * list[index] = 'dsafasfdsa'

2. Add

    * add element at the end

       list.append()

    * insert element into list

       list.insert(index, 'dfasfdsaf')

3. Delete elements

    * delete the element with index index at any position

       del list[index]

    * Pop the element at the end of the list

       lastElement = list.pop ()

    * Pop up the element whose index is index

       popedElement = list.pop(index)

    * remove element by value

       list.remove('sdfsafdsa')

       remove() removes only the first specified value. If the value to be removed may appear multiple times in the list, a loop needs to be used to determine if all such values ​​have been removed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324824419&siteId=291194637