python

To share this page click on the buttons below;

Lists

List are an ordered collection of elements. A python list can contain different type of elements within the same list.

Lists are created with

<mylist> = []

Access

To access the last elements into a list use negative indexes: <mylist>[-1] access the last element ans so forth.

Add

To add element to a list use:

<mylist>.append(<new element>);

If a list is appended to another list the appended list become a sub-list of the original one.

To add a list at the end of another use (in this case if the is a list then the elements of become elements of .)

<mylist>.extend(<new element>);

Remove

To remove all elements from a list:

<mylist>.clear()

To reemove element by index

<mylist>.pop(<index>)

that returns the removed element

To remove element by value

<mylist>.remove(<element>)

To remove by deleting

del <mylist>[<index>];

also slices of items can be deleted

del <mylist>[<start>:<stop>];

and it is possible to use a step inside a slice

del <mylist>[<start>:<step>:<stop>];

To share this page click on the buttons below;