List in new python version 3.10.x with new features

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-
    list = list()
  2. Using square bracket-
    list = []

List can have items of different data types.

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



[’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()

Note- (# means ‘Output’)

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

# [’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

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


# [’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’)

# [’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])


  • 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]

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

#it’s also same

o_nd_m= fruits[1:3]


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

#[‘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:]


#[’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'

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

  • Remove ()

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

# syntax
lst = ['item1', 'item2']

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



# [’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’]


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

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

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]

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

del fruits[1:3]

# [’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']

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

# []

  • 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()

# ['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


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

  • extend ()

num1 = [0, 1, 2, 3]
num2= [4, 5, 6]
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']

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


# 1

  • index ()

Index method returns an index position of an item.

# syntax
lst = ['item1', 'item2']

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


# 1

  • reverse ()

It provides a reverse list.

# syntax
lst = ['item1', 'item2']

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

# [’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']

# ascending

# descending


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

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

# [’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🙏




Blogger, College student

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Finding Pizza Places in Massachusetts using OSM REST API and Web Scrapping techniques

Need of Abstract Classes and Interfaces in Java

Docker: Essential in 5 minutes

How to use Genymotion, Appium & Applitools to test Android apps

Firebase + Heroku benefits

Apache Cassandra: Cassandra Best Practices

Home Assistant install with docker-compose

Learning Python: Complete Hero

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Saurabh Singh

Saurabh Singh

Blogger, College student

More from Medium

Best Rice Milling process and benefits of Rice Mill Software.

Rice mill Software

P-Fuel for Peloton — The personalized nutritionist at your fingertips / Personalized nutrition…

Thread in Java…!

CardanoPy: Operate and Extend Cardano Nodes Using a Python CLI