DEV Community

Cover image for Lists in Python Part-2
Aditi Jha for eduAlgo

Posted on

Lists in Python Part-2

Operations and Methods on Lists

Python has a lot of list methods that allow us to work with lists. Some of the methods are:
Python List append()- The append() method adds an item to the end of the list. The syntax of the append() method is: list.append(item). The method takes a single argument.

# names list
names = ['adi', 'prerna', 'ram']
# 'shyaam' is appended to the names list
names.append('shyaam')
# Updated namess list
print('Updated names list: ',names)
Enter fullscreen mode Exit fullscreen mode

Output: ['adi', 'prerna', 'ram', 'shyaam']

Python List extend()- The extend() method adds all the elements of an iterable (list, tuple, string etc.) to the end of the list. The syntax of the extend() method is: list1.extend(iterable).

languages = ['Hindi', 'English']
# another list of language
languages1 = ['Bengali', 'Tamil']
# appending language1 elements to language
languages.extend(languages1)
print('Languages List:', languages)
Enter fullscreen mode Exit fullscreen mode

Output: Languages List: ['Hindi', 'English', 'Bengali', 'Tamil']

Python List insert()- The list insert() method inserts an element to the list at the specified index. The syntax of the insert() method is: list.insert(i, a). Here, a is inserted to the list at the ith index. All the elements after ‘a’ are shifted to the right.

# vowel list
vowel = ['a', 'e', 'i', 'u']
# 'o' is inserted at index 3
# the position of 'o' will be 4th
vowel.insert(3, 'o')
print('Updated List:', vowel)
Enter fullscreen mode Exit fullscreen mode

Output: Updated List: ['a', 'e', 'i', 'o', 'u']

Python List remove()- The remove() method removes the first matching element (which is passed as an argument) from the list. The syntax of the remove() method is: list.remove(element). The remove() method takes a single element as an argument and removes it from the list. If the element doesn't exist, it throws ValueError: list.remove(x): x not in list exception.

# animals list
animals = ['cat', 'dog', 'rabbit', 'guinea pig']

# 'rabbit' is removed
animals.remove('rabbit')

# Updated animals List
print('Updated animals list: ', animals)
Enter fullscreen mode Exit fullscreen mode

Output:Updated animals list: ['cat', 'dog', 'guinea pig']

Python List pop()- The pop() method removes the item at the given index from the list and returns the removed item. The syntax of the pop() method is: list.pop(index). The method takes a single argument.

list1 = [ 1, 2, 3, 4, 5, 6 ] 
# Pops and removes the last element from the list 
print(list1.pop()) 
# Print list after removing last element 
print("New List after pop : ", list1, "\n") 
list2 = [1, 2, 3, ('cat', 'bat'), 4] 
# Pop last two element 
print(list2.pop()) 
print(list2.pop())
print(list2.pop())
# Print list 
print("New List after pop : ", li)
Enter fullscreen mode Exit fullscreen mode

Output: 6
New List after pop : [1, 2, 3, 4, 5]
4
('cat', 'bat')
3
New List after pop : [1, 2]

Python List reverse()- The reverse() method reverses the elements of the list. The syntax of the reverse() method is: list.reverse(). It doesn't return any value, it just updates the existing list.

# fruits List
fruits = ['apple', 'banana', 'orange']
print('Original List:', fruits)
# List Reverse
fruits.reverse()
# updated list
print('Updated List:', fruits)
Enter fullscreen mode Exit fullscreen mode

Output: Original List: ['apple', 'banana', 'orange']
Updated list: ['orange', 'banana', 'apple']

Python List Clear()- The clear() method removes all items from the list. The syntax of clear() method is: list.clear().

# Defining a list
list = [{1, 2}, ('a'), ['1.1', '2.2']]
# clearing the list
list.clear()
print('List:', list)
Enter fullscreen mode Exit fullscreen mode

Output: List: []

Python List sort()- The sort() method sorts the elements of a given list in a specific ascending or descending order. The syntax of the sort() method is: list.sort(key=..., reverse=...). Alternatively, you can also use Python's built-in sorted() function for the same purpose. The syntax is: sorted(list, key=..., reverse=...).

# vowels list
vowels = ['e', 'a', 'u', 'o', 'i']
# sort the vowels
vowels.sort()
# print vowels
print('Sorted list:', vowels)
Enter fullscreen mode Exit fullscreen mode

Output: Sorted list: ['a', 'e', 'i', 'o', 'u']

The simplest difference between sort() and sorted() is: sort() changes the list directly and doesn't return any value, while sorted() doesn't change the list and returns the sorted list.

Built-In functions of the Lists

  1. len()- returns the length of the list. Syntax: list.len()
  2. sum()- returns the sum of the numbers in the list. Syntax: list.sum()
  3. insert()- Inserts object obj into list at offset index. Syntax: list.insert(index, obj)
  4. count()-Returns count of how many times obj occurs in list. Syntax: list.count(obj)
  5. max()- Returns item from the list with max value. Syntax: list.max()
  6. min()- Returns item from the list with min value. Syntax: list.min()

Lists in python is a very important concept which plays an important role while learning the basics of python programming. Python programming language has many out of the box features, with the applications and implementations it has become one of the most popular programming language nowadays.

Find the part-1 of this blog here.

Top comments (0)