Net-informations.com

Python List


Python Lists and various List operations

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.



python list operations

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

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

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


Inserting elements in python 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


Python list slice

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

python 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

How to remove duplicates from a Python List

The common approach to get a unique collection of items is to use a dictionary. A Python dictionary is a mapping of unique keys to values. So, converting Python list to dictionary will automatically remove any duplicates because dictionaries cannot have duplicate keys .

example

output

Python list extend method()

The list extend() method extends the list by adding all items of a list (passed as an argument) to the end.

example

output

Difference between list methods append() and extend()?

Difference between python list append() method and python list extend() method

Python append() method adds an element to a list, and the extend() method concatenates the first list with another list (or another iterable). When append() method adds its argument as a single element to the end of a list, the length of the list itself will increase by one. Whereas extend() method iterates over its argument adding each element to the list, extending the list. The length of the list will increase by however many elements were in the iterable argument.

Python append() example

output

Python extends() example

output

Python append() Vs. extend() Operator Overloading

In python, both + and += operators are defined for list. They are semantically similar to extend. first_list + second_list creates a third_list in memory, so you can return the result of it, but it requires that the second iterable be a list. first_list += second_list modifies the list in-place (it is the in-place operator, and lists are mutable objects ) so it does not create a new list. It also works like extend, in that the second iterable can be any kind of iterable.

Time Complexity

  1. Append has constant time complexity i.e.,O(1).
  2. Extend has time complexity of O(k). Where k is the length of list which need to be added.

Python append() or extend()?

The extend() method is semantically clearer, and that it can run much faster than append, when you intend to append each element in an iterable to a list. on the other hand, if you only have a single element (not in an iterable) to add to the list, better to use append() method .










net-informations.com (C) 2019    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About