Code in Python
Machine Learning and Deep Learning Blogs
search

Learn about Python List and its Built-in Functions

A Python list is a sequence data type similar to array. It contains characters(numbers, letters, words) separated by commas and enclosed in square brackets ([]). A list can consist of all elements as numbers, characters or a combination of both. The arrangement of the elements in a list is not specified (it can be sorted or unsorted). There is a concept of order in lists. Every element has a particular index. The element can be called or deleted or replaced by another number using the index of that element.

Python List – Built-in Functions

There are some built-in functions used to perform various operations on lists.

1. append(x) – append() is used to add item x at the end of the list. If you add an element to the list using (+) sign, then the element will get added to the list for the particular statement. But, if you add the element using append(), then the element will get permanently added to the list.

2. extend(L) – This built-in function is used to add all items in given list L to the end. It permanently adds up the elements of one list to another list.

3. insert(i , x) – This built-in function is used to insert item x at position i in the list.

4. remove(x) – This built-in function is used to remove the item that is equal to x from the list.

5. clear() – This built-in function is used to remove all items and empty the list.

6. index(x) – This built-in function is used to return the index of item x. It will return the index of the first occurrence of x if item x occurs more than once in the given list.

7. count(x) – This built-in function is used to return a number of items that is equal to x. Basically, it will count the number of times x occurs in the list and will return that count.

8. sort() – This built-in function is used to sort items in a list in ascending order.

9. reverse() –This built-in function is used to reverse the order of items in the list.

10. copy() – This built-in function is used to return a shallow copy of the list.

Example –


>>> a = [1, 4, 3, 7, 9, 3, 65, 9, 98, 34]
>>> a.append(45)
>>> print(a)
[1, 4, 3, 7, 9, 3, 65, 9, 98, 34, 45]
>>> a.extend(a)
>>> print(a)
[1, 4, 3, 7, 9, 3, 65, 9, 98, 34, 1, 4, 3, 7, 9, 3, 65, 9, 98, 34]
>>> a.insert(4, 123)
>>> print(a)
[1, 4, 3, 7, 123, 9, 3, 65, 9, 98, 34]
>>> a.remove(123)
>>> print(a)
[1, 4, 3, 7, 9, 3, 65, 9, 98, 34]
>>> a.index(3)
>>> print(a)
2
>>> a.count(9)
>>> print(a)
2
>>> a.sort()
>>> print(a)
[1, 3, 3, 4, 7, 9, 9, 34, 65, 98]
>>> a.reverse()
>>> print(a)
[34, 98, 9, 65, 3, 9, 7, 3, 4, 1]
>>> a.copy()
>>>print(a)
[1, 4, 3, 7, 9, 3, 65, 9, 98, 34]
>>> a.clear()
>>> print(a)
[ ]

There are various operations that can be done on lists:

a) You can print out a particular element just by specifying its index. Remember index starts from zero.
b) You can print out a specific part of the list by slicing the list. Specify start index, end index and the number of steps.
c) You can print the list to a particular index.
d) You can get the number of elements of the list by len() function.
e) You can reverse a whole string by using [::-1]. Here, -1 refers to the direction of the list.

Example:


>>> a = [1,2,3,4,5,6,7,8,9]
>>> a[0]
1
>>> a[2:6]
[3,4,5,6]
>>> a[:6]
[1,2,3,4,5,6]
>>> a[2:]
[3,4,5,6,7,8,9]
>>> a[:]
[1,2,3,4,5,6,7,8,9]
>>> len(a)
9
>>> a[::-1]
[9,8,7,6,5,4,3,2,1]

Akarsh Singh
Follow me

Akarsh Singh

Founder, Code in Python. Web and Android App Developer. He loves Python and wants to share his knowledge via this website. Follow him on Facebook, Twitter and Github.
Akarsh Singh
Follow me

Latest posts by Akarsh Singh (see all)

2 Comments

  1. Wonderful site. Lots of useful information here. I am seending
    it to several buddies ans additionally sharin in delicious.
    And of course, thank you on your sweat!

  2. Good day my good friend! I must express that this informative article is definitely incredible, pleasant written and can include most considerable infos. I want to notice far more content such as this professional hosting.

Leave a Reply

Your email address will not be published.

*

© 2018 Code in Python