Learn to use Lists in Python

Lists are used to hold multiple objects together. It can be defined as a comma seperated value that allows us to access any element based on it's index. Unlike tuples Lists are mutable i.e. we can modify lists.

Lists are one of the four built-in data structures in Python. The four data structures in Python are:

  • Lists
  • Set
  • Dictionary
  • Tuple

In this article we'll discuss about lists.

Lists are ordered and they allow duplicate members. Lists are heterogeneous i.e. they can contain strings, characters, numbers etc. A single list can contain all numbers, strings and characters.

Declaration of Lists

In python lists are written with square brackets with elements written inside them. If the element is a string or character we declare it with double quotes.

List1 = ["Apples", "Mangoes", "Oranges"]

Accessing elements of a List

To access an element of a list we write the list name with the index number of the required element.

>>>List1 = ["Apples", "Mangoes", "Oranges"]
>>>print(List1[0])
Apples
>>>print(List1[2])
Oranges

Adding an element to a List

We use the append function to add any element to our list.

>>>List1 = ["Apples", "Mangoes", "Oranges"]
>>>List1.append("Grapes")
>>>print(List1)
['Apples','Mangoes','Oranges','Grapes']

Finding the length of the List

To find the number of elements in a list we use len() function.

>>>print(List1)
['Apples','Mangoes','Oranges','Grapes']
>>>len(List1)
4

Deleting an element from the List

We use the del() function to delete a particular element from the list.

>>>print(List1)
['Apples','Mangoes','Oranges','Grapes']
>>>del(List1[2])
>>>print(List1)
['Apples','Mangoes','Grapes']

Modifying elements in a List

To modify an element in a list we assign the new element to the index number of the old element.

>>>print(List1)
['Apples','Mangoes','Grapes']
>>>List1[2] = "Oranges"
>>>print(List1)
['Apples','Mangoes','Oranges']

Iteration of elements in a List

We use loops to iterate elements in a list.

>>>List = ["Apples","Mangoes","Oranges"]
>>>for i in list:
      print(i)
      
 Apples
 Mangoes
 Oranges

Adding two Lists

We can use '+' to add two lists.

>>>List1 = ["Apples", "Mangoes"]
>>>List2 = ["Rose", "Lilly"]
>>>print(List1 + List2)
['Apples','Mangoes','Rose','Lilly']

Check if a particular element exists in the List

To check if a particular element exists in a list we use loops.

>>>List = ["Apples","Mangoes","Oranges"]
>>>if "Oranges" in List:
          print("Yes")
          
Yes

Operations on Lists

To print a list i number of times we can directly multiply the list with i and print it.

>>>List = ["Mangoes", "Apples"]
>>>print(List * 2)
['Mangoes','Apples','Mangoes','Apples']

Finding the Minimum and Maximum elements of a List

We use the functions min() and max() to find the minimum and maximum elements of the list respectively.

  • For a list which contain numbers We get the minimum and maximum elements directly.
>>>List = [12,23,45,65,100]
>>>min(List)
12
>>>max(List)
100
  • For a list which contains strings

We get the minimum and maximum elements on the basis of alphabetical order.

>>>List = ["Apples","Mangoes","Oranges"]
>>>min(List)
'Apples'
>>>max(List)
'Oranges'
  • For a list which contains both numbers and strings

As we cannot compare an integer with a string we get an error if we try to find the minimum and maximum elements.

>>> List = [23,"Apples",100,"Mangoes"]
>>>min(List)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
min(List)
TypeError: '<' not supported between instances of 'str' and 'int'

Deleting the List

To completely delete the list we use the del() function.

>>> List=[12,23,34]
>>> print(List) 
[12, 23, 34]
>>> del(List)

After deleting the list if we further try to access the list we get an error which states that the list is not defined.

>>> List=[12,23,34]
>>> print(List)
[12, 23, 34]
>>> del(List)
>>> print(List)
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    print(List)
NameError: name 'List' is not defined

This is a companion discussion topic for the original entry at http://iq.opengenus.org/lists-in-python/