DEV Community

Cover image for List in Python
chetan jain
chetan jain

Posted on

List in Python

List

A list data structure is a collection of heterogeneous data elements (such as numbers or characters—or even other data structures) that are structured in some way, for example, by numbering (indexing) the elements. The most basic data structure in python is called list.

  • List is one of the Sequence Data structure.
  • List is a collection of items (Strings, integers or even other lists).
  • Lists are enclosed in [ ].
  • Each item in the list has an assigned index value.
  • Each item in a list is separated by a comma.
  • Lists are mutable, which means they can be changed.

List Creation

emptyList = []
lst = ['one', 'two', 'three', 'four'] # list of strings
lst2 = [1, 2, 3, 4] #list of integers
lst3 = [[1, 2], [3, 4]] # list of lists
lst4 = [1, 'ramu', 24, 1.24] # list of different datatypes

List Length

lst = ['one', 'two', 'three', 'four']
#find length of a list
print(len(lst)) # this will return 4

List Append

lst = ['one', 'two', 'three', 'four']
lst.append('five') # append will add the item at the end
print(lst) # print ['one', 'two', 'three', 'four', 'five']

List Insert

# Syntax: lst.insert(x, y) 
lst = ['one', 'two', 'four']
lst.insert(2, "three") # will add element y at location x
print(last) # print ['one', 'two', 'three', 'four']

Append vs Insert

Append only add an element at the end of the list but insert add an element at any position in the list.

List Remove

# Syntax: lst.remove(x) 
lst = ['one', 'two', 'three', 'four', 'two']
lst.remove('two') #it will remove the first occurrence of 'two' in a given list
print(last) # print ['one', 'three', 'four', 'two']

List Append & extend

lst = ['one', 'two', 'three', 'four']
lst2 = ['five', 'six'] 
lst.append(lst2) # append 
print(lst) # print ['one', 'two', 'three', 'four', ['five', 'six']]

#extend will join the list with list1
# Syntax: lst.extend(lst2)
lst.extend(lst2) # extend
print(lst) # print ['one', 'two', 'three', 'four', 'five', 'six']

List Delete vs Pop

#del to remove item based on index position
lst = ['one', 'two', 'three', 'four', 'five']
del lst[1] # remove 'two' from the list
print(lst) # print ['one', 'three', 'four', 'five']

#or we can use pop() method
a = lst.pop(1) # this will remove 'three' because 'two' is already deleted
print(lst) # print ['one', 'four', 'five']

List related Keywords in Python

in

#keyword 'in' is used to test if an item is in a list.
lst = ['one', 'two', 'three', 'four']
if 'two' in lst:
    print('AI') # print 'AI' because 'two' is present in the list.

#keyword 'not' can combined with 'in'
if 'six' not in lst:
    print('ML') # print 'ML' because 'six' is not present in the list.

List Reverse

#reverse will be reversed the entire list
lst = ['one', 'two', 'three', 'four']
lst.reverse() 
print(lst) # print ['four', 'three', 'two', 'one']

List Sorting

  • The easiest way to sort a list is with the sorted(list) function.
  • That takes a list and returns a new list with those elements in sorted order.
  • The original list is not changed.
  • The sorted() optional argument reverse=True, e.g. sorted(list, reverse=True), makes it sort backwards.
#create a list with numbers
numbers = [3, 1, 6, 2, 8]
sorted_lst = sorted(numbers) 
print("Sorted list :", sorted_lst) # print [1, 2, 3, 6, 8] 
# (original list remained unchanged)

#print a list in reverse sorted order
print("Reverse sorted list :", sorted(numbers, reverse=True)) 
# print [8, 6, 3, 2, 1]

#sort the list and stored in itself
numbers.sort() # this will change the original list [1, 2, 3, 6, 8]
print(numbers) # print [1, 2, 3, 6, 8]

The list is robustly used in python so please read and I will add some more things of a list in next post like slicing and list comprehension and some more thing.
Till then bye.
Thank you for reading

Top comments (0)