List in new python version 3.10.x with new features

Saurabh Singh
5 min readApr 30, 2022

List: is a collection which is orde-
-red and changeble (modifiable) Allows duplicate members.

  • A list can be empty [] or it may have different data type items
  • List is mutable(add and remove items)
  • List support indexing, slicing and sorting

— — How to create a list — —

In python we can create list in two ways:

  1. Using list built-in function-
    #syntax
    list = list()
  2. Using square bracket-
    #syntax
    list = []

List can have items of different data types.

lst = [“string”,20,10.5,True,]

Print(lst)

#output

[’string’, 20, 10.5, True]

The list data type has some more methods. Here are all of the methods of list objects:

  • list.append(x)

To add item to the end of an existing list we use the method append().

# syntax

lst = list()
lst.append(item)

Note- (# means ‘Output’)

fruits = [’banana’, 'orange’, 'mango’, 'lemon’]
fruits.append(’apple’)
print(fruits)

# [’banana’, 'orange’, 'mango’, 'lemon’, 'apple’]

According to my requirements, want to add multiple values with the help of append method then we need to difine an variable
fruits.append(’lichi’,’grapes’)

#Occurs an Error
var = [‘lichi’,‘grapes’]

fruits.append(var)

# [’banana’, 'orange’, 'mango’, 'lemon’, 'apple’, [‘lichi’,‘grapes’]]

  • list.insert(x)

We can use insert() method to insert a single item at a specified index in a list. Note that other items are shifted to the right. The insert() methods takes two arguments:index and an item to insert.

# syntax
lst = [’item1’, 'item2’]
lst.insert(index, item)

fruits = [’banana’, 'orange’, 'mango’, 'lemon’]
fruits.insert(2, 'apple’)
print(fruits)

# [’banana’, 'orange’, 'apple’, 'mango’, 'lemon’]

  • len(list)

Lists with initial values. We use len() to find the length of a list.

length = len(fruits)

Print(length) #5

  • Indexing

We access each item in a list using their index. A list index starts from 0. See below where the index starts

fruits = [’banana’, 'orange’, 'apple’,’mango’, 'lemon’]

print (fruits[0])

# banana
Print(fruits [4])

# lemon

  • Negative indexing

Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item.

fruits = [’banana’, 'orange’, 'apple’,’mango’, 'lemon’]

print (fruits[-1])

# lemon
print (fruits [-5])

#banana

  • Unpacking

unpacking means we can assign (store) values in a particular variable

lst = [’item1’,’item2’,’item3’, 'item4’, 'item5’]

first_item, second_item, third_item, *rest = lst

print(first_item) # item1
print(second_item) # item2
print(third_item) # item3
print(rest) # [’item4’, 'item5’]

  • Slicing

Positive Indexing: We can specify a range of positive indexes by specifying the start, end and step, the return value will be a new list. (default values for start = 0, end = len(lst) - 1 (last item), step = 1)

fruits = [’banana’, 'orange’, 'mango’, 'lemon’]
all_fruits = fruits[0:4]
Print(all_fruits)

[’banana’, 'orange’, 'mango’, 'lemon’]
all_fruits = fruits[0:]

#it’s also same

o_nd_m= fruits[1:3]

Print(o_nd_m)

#[’orange’, 'mango’]
o_m_l = fruits[1:]
Print(o_m_l)

#[‘orange’, 'mango’, 'lemon’]

Negative Indexing: We can specify a range of negative indexes by specifying the start, end and step, the return value will be a new list.

o_m_l= fruits[-3:]

Print(o_m_l)

#[’orange’, 'mango’, 'lemon’]

reverse_fruits = fruits[::-1]

print (reverse_fruits)

#[’lemon’, 'mango’, 'orange’, 'banana’]

  • Modifying

List is a mutable or modifiable ordered collection of items. Lets modify the fruit list.

fruits = [’banana’, 'orange’, 'mango’, 'lemon’]
fruits[0] = 'avocado'
print(fruits)

# [’avocado’, 'orange’, 'mango’, 'lemon’]

  • Remove ()

Removing Items from a List
The remove method removes a specified item from a list

# syntax
lst = ['item1', 'item2']
lst.remove(item)

fruits = [’banana’, 'orange’, 'mango’, 'lemon’, 'banana’]

fruits.remove(’banana’)

print(fruits)

# [’orange’, 'mango’, 'lemon’, 'banana’]

Note - this method removes the first occurrence of the item in the list.

  • Pop()

Removing Items Using Pop
The pop() method removes the specified index, (or the last item if index is not specified):

# syntax
lst = [’item1’, 'item2’]
lst.pop()

#item1

fruits = [’banana’, 'orange’, 'mango’, 'lemon’]
fruits.pop()
print(fruits)

# [’banana’, 'orange’, 'mango’]

fruits.pop(0)
print(fruits) # ['orange', 'mango']

  • Del

Removing Items Using Del
The del keyword removes the specified index and it can also be used to delete items within index range. It can also delete the list completely

# syntax
lst = [’item1’, 'item2’]
del lst[index]

fruits = [’banana’, 'orange’, 'mango’, 'lemon’, 'kiwi’, 'lime’]
del fruits[0]
print(fruits)

# [’orange’, 'mango’, 'lemon’, 'kiwi’, 'lime’]

del fruits[1:3]
print(fruits)

# [’orange’, 'lime’]

del fruits
#it will be deleted the whole list structure
print(fruits) # Error !!

  • clear()

The clear() method empties the list:

# syntax
lst = ['item1', 'item2']
lst.clear()

fruits = ['banana', 'orange', 'mango', 'lemon']
fruits.clear()
print(fruits)

# []

  • copy ()

copy() method make a copy that means to assign the whole values to another variable

# syntax
lst = ['item1', 'item2']
lst_copy = lst.copy()

fruits = ['banana', 'orange', 'mango', 'lemon']
fruits_copy = fruits.copy()
print(fruits_copy)

# ['banana', 'orange', 'mango', 'lemon']

  • Joining List

There are several ways to join or concatenate the lists in python.

  • Plus Operator (+)
    # syntax
    list3 = list1 + list2

    Joining using extend() method The extend() method allows to append list in a list. See the example below.
    # syntax
    list1 = [’item1’, 'item2’]
    list2 = [’item3’, 'item4’, 'item5’]

list = list1+ list2 + list3 + list4 + list5

Print(list)

# [’item1’, 'item2’item3’, 'item4’, 'item5’]

  • extend ()

num1 = [0, 1, 2, 3]
num2= [4, 5, 6]
num1.extend(num2)
print(’Numbers:’, num1)

# Numbers: [0, 1, 2, 3, 4, 5, 6]

  • count()

Counting Items in a List
The count() method returns the number of times an item appears in a list:

# syntax
lst = ['item1', 'item2']
lst.count(item)

fruits = ['banana', 'orange', 'mango', 'lemon']

print(fruits.count('orange'))

# 1

  • index ()

Index method returns an index position of an item.

# syntax
lst = ['item1', 'item2']
lst.index(item)

fruits = ['banana', 'orange', 'mango', 'lemon']

print(fruits.index(’orange’))

# 1

  • reverse ()

It provides a reverse list.

# syntax
lst = ['item1', 'item2']
lst.reverse()

fruits = [’banana’, 'orange’, 'mango’, 'lemon’]
fruits.reverse()
print(fruits)

# [’lemon’, 'mango’, 'orange’, 'banana’]

  • sorting ()

The sort() method reorders the list items in ascending order and modifies the original list. If an argument of sort() method reverse is equal to true, it will arrange the list in descending order.

sort(): this method modifies the original list

# syntax
lst = ['item1', 'item2']
lst.sort()

# ascending
lst.sort(reverse=True)

# descending

Example:

fruits = ['banana', 'orange', 'mango', 'lemon']
fruits.sort()
print(fruits)

# [’banana’, 'lemon’, 'mango’, 'orange’]
fruits.sort(reverse=True)
print(fruits)

# [’orange’, 'mango’, 'lemon’, 'banana’]

In this article, we analyzed the List data type of Python. I hope We will discuss more data types in Python in the future. Until then, have fun, read, learn and I hope you are enjoying in in this article so keep coding,

Thanks for wisiting🙏

--

--