Access items in the List

Since the List is indexed by default, you can access the List by subscript, as shown in the following code:


thislist = ["apple"."banana"."cherry"]
print(thislist[1])

Copy the code

Negative index

A negative index means counting backwards, for example:

  • Minus 1 is the last term
  • Minus 2 is the second to last term

Here’s an example:


thislist = ["apple"."banana"."cherry"]
print(thislist[-1])

PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
cherry

Copy the code

The index range

You can indicate an index range by specifying the start and end values. Once the range is specified, a new collection is cut out of the collection, as shown in the following code:


thislist = ["apple"."banana"."cherry"."orange"."kiwi"."melon"."mango"]
print(thislist[2:5])


PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
['cherry'.'orange'.'kiwi']

Copy the code

Index of negative range

As mentioned earlier, negative numbers are calculated from the tail of the set forward. The following example shows how to cut small sets from the fourth to last position – the first to last position.


thislist = ["apple"."banana"."cherry"."orange"."kiwi"."melon"."mango"]
print(thislist[-4: -1])

PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
['orange'.'kiwi'.'melon']

Copy the code

Check whether the collection item exists

You can use the in keyword to determine whether an item exists in the set, as shown in the following code:


thislist = ["apple"."banana"."cherry"]
if "apple" in thislist:
  print("Yes, 'apple' is in the fruits list")

PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
Yes, 'apple' is in the fruits list

Copy the code

The new item

You can use the append() function to insert an item at the end of a list.


thislist = ["apple"."banana"."cherry"]
thislist.append("orange")
print(thislist)


PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
['apple'.'banana'.'cherry'.'orange']

Copy the code

Insert item

To insert an item into a list, use the insert() function.


thislist = ["apple"."banana"."cherry"]
thislist.insert(1."orange")
print(thislist)

PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
['apple'.'orange'.'banana'.'cherry']

Copy the code

To expand the List

You can merge all the elements in one collection into another, as shown in the following code:


thislist = ["apple"."banana"."cherry"]
tropical = ["mango"."pineapple"."papaya"]
thislist.extend(tropical)
print(thislist)

PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
['apple'.'banana'.'cherry'.'mango'.'pineapple'.'papaya']

Copy the code

As you can see, all the elements in the Tropical collection are added to the thisList collection.

Add any iterable collection

Iterable collections are generally collections that support for in operations. In addition to lists, there are (tuple,set,dictionary), etc. Here is an example of adding a tuple to a list as a whole.


thislist = ["apple"."banana"."cherry"]
thistuple = ("kiwi"."orange")
thislist.extend(thistuple)
print(thislist)

PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
['apple'.'banana'.'cherry'.'kiwi'.'orange']

Copy the code

Modify the item

If you want to change the value of the specified position in the list, you can change it directly using the array index as follows:


thislist = ["apple"."banana"."cherry"]
thislist[1] = "blackcurrant"
print(thislist)

PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
['apple'.'blackcurrant'.'cherry']

Copy the code

Modify a value in the list

This feature is not available in many programming languages. We know that common changes are made item by item, but can we make imaginative, paragraph by paragraph changes? Yes, specify the range of the data on the left, and assign a new value to the range on the right, as shown in the following code:


thislist = ["apple"."banana"."cherry"."orange"."kiwi"."mango"]
thislist[1:3] = ["blackcurrant"."watermelon"]
print(thislist)


PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
['apple'.'blackcurrant'.'watermelon'.'orange'.'kiwi'.'mango']

Copy the code

At this point, the smart thing to think about is, what if the number of sets on the right is larger than your interval on the left? The current practice is to insert extra items into the list, resulting in more items in the list than before, as shown in the following code:


thislist = ["apple"."banana"."cherry"]
thislist[1:2] = ["blackcurrant"."watermelon"]
print(thislist)

PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
['apple'.'blackcurrant'.'watermelon'.'cherry']

Copy the code

Delete the item

To remove an item from a list, use the remove() function.


thislist = ["apple"."banana"."cherry"]
thislist.remove("banana")
print(thislist)

Copy the code

Deletes the item at the specified location

In addition to deleting by element value, you can also delete by specifying the index position, as shown in the following code:


thislist = ["apple"."banana"."cherry"]
thislist.pop(1)
print(thislist)

Copy the code

In addition to the above two, you can also remove them using Python’s built-in del keyword.


thislist = ["apple"."banana"."cherry"]
del thislist[0]
print(thislist)

PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
['banana'.'cherry']

Copy the code

To empty the list

Python’s built-in clear method clears the list collection. This means that the list still exists, but there is no item in it, as shown in the following code:


thislist = ["apple"."banana"."cherry"]
thislist.clear()
print(thislist)

PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py
[]

Copy the code

Link: www.w3schools.com/python/pyth…

For more high-quality dry goods: See my GitHub:python