Python lists are ordered list of items or elements that can be altered anytime after declaration. This post shows examples of using some of the useful built in functions that are used on Lists. You will find here some Quick examples of using Lists and its inbuilt functions. These examples should come in handy for a quick reference to pick up, so long as you know what exactly you want out of this.

Create and print a List in python

>>> arr = [1, "doc", "ppt"]

>>> print(arr)
[1, 'doc', 'ppt']
>>>
>>> print(arr[0])
1
>>> print(arr[2])
ppt

Print elements of list in a nicer format using ‘for’ loop

>>> arr = [1, "doc", "ppt"]
>>> for i in arr: print i
...
1
doc
ppt
>>>

Remove an element from python list

# please note it removes only the first occurance
>>> a = ['a', 'b', 'c', 'd', 'b']
>>> a.remove('b')
>>> print a
['a', 'c', 'd', 'b']

Remove all occurances of an element in Python list

>>> for i in a:
...     if i == 'b':
...        a.remove(i)
...
>>>
>>> print(a)
['a', 'c', 'd']
>>>

Remove elements even if our search string is a substring of an element in list

>>> a = ['q', 'b', 'c', 'd', 'qq']
>>> for i in a:
...     if 'q' in i:
...       a.remove(i)
...
>>> print(a)
['b', 'c', 'd']
>>>

Delete elements based on index

>>> a = ['a', 'b', 'c', 'd', 'ab']
>>> a.pop(0)
'a'
>>> print(a)
['b', 'c', 'd', 'ab']

Insert an element at a specific index in a List

>>> myList.insert(0,"Yes")
>>> myList
['Yes', 'The', 'earth', 'revolves', 'around', 'sun']

Append an element to the end of list

>>> a = ['a', 'b', 'c', 'd', 'ab']
>>> a.append("jj")
>>> print a
['a', 'b', 'c', 'd', 'ab', 'jj']

Append multiple elements to a list using extend

>>> a = ['a', 'b', 'c', 'd', 'ab']
>>> a.extend(["12", "hj"])
>>> print(a)
['a', 'b', 'c', 'd', 'ab', '12', 'hj']

Using mathematical operator on lists to add an element to array

>>> myList
['The', 'earth', 'revolves', 'around', 'sun', 'for']
>>> myList = myList + ["sure"]

>>> myList
['The', 'earth', 'revolves', 'around', 'sun', 'for', 'sure']
>>> myList += ["."]
>>> myList
['The', 'earth', 'revolves', 'around', 'sun', 'for', 'sure', '.']

>>> myList *= 2
>>> myList
['The', 'earth', 'revolves', 'around', 'sun', 'for', 'sure', '.', 'The', 'earth', 'revolves', 'around', 'sun', 'for', 'sure', '.']
So we see that through + operator elements could be added to the list and through * operator we can add the complete list repeatedly towards the end.

Get the length of list

>>> print a
['a', 'b', 'c', 'd', 'ab', 'jj']
>>>
>>> len(a)
6
>>>

Search an element in list and get its index

>>> print a
['a', 'b', 'c', 'd', 'ab', '12', 'hj']
>>> a.index('12')
5
>>>

Check if an element is found or not in a list

>>> print a
['a', 'b', 'c', 'd', 'ab', '12', 'hj']
>>>
>>> "c" in a
True
>>>
>>> "88" in a
False
>>>

Search and replace string or substring if it matches search string

>>> a = ['a', 'b', 'c', 'd', 'ab']
>>> a = [w.replace('a', 'x') for w in a]
>>> print a
['x', 'b', 'c', 'd', 'xb']

Search and replace string only if it matches element, and ignores substrings if found

>>> a = ['a', 'b', 'c', 'd', 'ab']
>>> for i in a:
...     if i == 'a':
...         pos = a.index(i)
...         a.insert(pos,"x")
...         pos = pos+1
...         a.pop(pos)
...
'a'
>>> print a

Count the number of occurances of an element

>>> mylist = ['a', 'f', 'w', 'w', 'w', 2, 1]
>>> print(mylist.count('w'))
3
>>>

Sort the list in Ascending or Desending order

# Sort Ascending
>>> mylist = ['a', 'f', 'w', 'w', 'w', 2, 1]
>>> print mylist
['a', 'f', 'w', 'w', 'w', 2, 1]
>>>
>>> mylist.sort()
>>> print mylist
[1, 2, 'a', 'f', 'w', 'w', 'w']
>>>

# Sort Desending
>>> mylist = ['a', 'f', 'w', 'w', 'w', 2, 1]
>>> print mylist
['a', 'f', 'w', 'w', 'w', 2, 1]
>>> mylist.sort(reverse=True)
>>> print mylist
['w', 'w', 'w', 'f', 'a', 2, 1]
>>>

Clear or empty all elements in a list

# clear() works only with python 3
>>> mylist = ['a', 'f', 'w', 'w', 'w', 2, 1]
>>> print mylist
['a', 'f', 'w', 'w', 'w', 2, 1]
>>> mylist.clear()
>>> print mylist
[]

# Achieve the same using del in python 2
>>> mylist = ['a', 'f', 'w', 'w', 'w', 2, 1]
>>> print mylist
['a', 'f', 'w', 'w', 'w', 2, 1]
>>> del mylist[:]
>>> print mylist
[]
>>>

This site uses Akismet to reduce spam. Learn how your comment data is processed.