Net-informations.com
SiteMap  | About    

Python List

Python List is one of the most frequently used and very versatile datatype used in Python. In Python, lists are objects and it holds a number of other objects. Lists are very similar to arrays. It implements the sequence protocol, and also allows you to add and remove objects from the sequence. List literals are written within square brackets [ ] with the first element at index 0. There are many methods associated to them. Some of which are presented here below.

Creating List

Creating a list is as simple as putting different comma-separated values in square brackets.

Accessing List Values

The syntax for accessing the elements of a list is the same as the syntax for accessing the characters of a string. The expression inside the brackets specifies the index. Python indexes starts its lists at 0 rather than 1.

example

output

example

output

List length

The function len returns the length of a list, which is equal to the number of its elements.

example

output

Clear or Emptying List

list.clear() remove all items from the list.

example

output

How to create an Excel Document Programmatically

Inserting and Removing Elements

append() - Appends adds its argument as a single element to the end of a list. The length of the list itself will increase by one.

example

output

Appending a list inside a list

example

output

How to create an Excel Document Programmatically

List operations

Using the "+" operator concatenates lists.

example

output

using the * operator repeats a list a given number of times.

example

output

example

output

Inserting elements in List

example

output

example

output

Remove lements from List

example

output

List Count

list.count(x) return the number of times x appears in the list.

example

output

Slice Elements

Python slice extracts elements, based on a start and stop.

example

output

str[1:4] - The 1 means to start at second element in the list (note that the slicing index starts at 0). The 4 means to end at the fifth element in the list, but not include it. The colon in the middle is how Python's lists recognize that we want to use slicing to get objects in the list.

example

output

example

output

List Reverse

The reverse() method in list reverse the elements of the list in place.

example

output

List index()

The index() method returned the index of the first matching item.

example

output

If you want to specify a range of valid index, you can indicate the start and stop indices:

example

output

Exist in List

We can test if an item exists in a list or not, using the keyword "in"

example

output

not in List

example

output

Create new List with Dynamic Values

example

output

List sort

List sort() method that performs an in-place sorting

example

output

Reverse Sorting

example

output

List as Stack

A stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. In the pushdown stacks only two operations are allowed: push the item into the stack, and pop the item out of the stack. Here to add an item to the top of the List stack, use append() (push) and to retrieve an item from the top of the stack, use pop() without an explicit index.

example

output

Lists as Queues

A queue is a container of objects that are inserted and removed according to the first-in first-out (FIFO) principle. In the queue only two operations are allowed enqueue and dequeue. Enqueue (append()) means to insert an item into the back of the queue, dequeue (pop(0)) means removing the front item.

example

output

Iterating Through a List

Using a for loop we can iterate though each item in a list.

example

output

In order to get every other item, starting with the first.

example

output

Get every other item, starting with the second.

example

output

Reverse items

example

output

zip() function

To loop over two or more sequences at the same time, the entries can be paired with the zip() function.

example

output









net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.