Python Lists with example

List in Python or Python Lists is a built-in data type of Python or collection data types of Python programming. It is used to store multiple values in a single variable. Also, we can store multiple values of different data types in a list variable such as integer, string, boolean, etc. It is declared by using the second bracket.

Also read, Variables in Python with Examples

Basically, Python Lists are of three types as given below

  • Ordered:- In the ordered list, items are arranged in a fixed order that can not be changed. If a new item is added, it will be placed at the end of the list values.
  • Changeable:- A list is changeable which means we can change the order of items in the list, add new items or remove items from the list variable.
  • Allow Duplicates:- We can add duplicate values in a list and the same values are accessed according to the index as the index starts from the position ‘0’.

Below are some examples of List Example 1:-

carlist = ["Maruti","Volvo","BMW","Toyota"] 
print(carlist)

Output:
[‘Maruti’, ‘Volvo’, ‘BMW’, ‘Toyota’]

Example 2:-

mylist = ["xyz",23,"abc",10.5,False] 
print(mylist)

Output:
[‘xyz’, 23, ‘abc’, 10.5, False]

Example 3:-

mylist = [1,2,3,4,4] 
print(mylist)

Output:
[1, 2, 3, 4, 4]

Length of the list:-

We can find the length of the list which means the total number of items or values in a list with the help of len() function as shown below

Example 4:-

mylist = [1,2,3,4,5] 
print(len(mylist))

Output:-
5

List Constructor in Python

Also, we can store multiple values in a single variable using the list() constructor as shown below

Example 5:-

mylist = list(("maruti","volvo","BMW","Toyota")) 
print(mylist)

Output:
[‘maruti’, ‘volvo’, ‘BMW’, ‘Toyota’]

Use the double-rounded bracket while using the list constructor.

Access Items in a List:- ( Python)

We can access items in a list by using the index numbers from the list as you know the index starts from zero.

Example 6:-

mylist = ["maruti","volvo","BMW","Toyota"] 
print(mylist[2])

Output:
BMW

Access Items in a List using a negative index

We can also access any item in a list using the negative index numbers such as

  • ‘-1’ indicates the last item from the list
  • ‘-2’ indicates the second last item from the list
  • ‘-3’ indicates the third last item from the list

Example 7:-

mylist = ["maruti","volvo","BMW","Toyota"] 
print(mylist[-3])

Output:
volvo

Access items in a list using a range of index

We can also access the items in a list using the range of index just by putting the start index number and end index number as shown below

Example 8:-

mylist = ["maruti","volvo","BMW","Toyota"] 
print(mylist[1:3])

Output:
[‘volvo’, ‘BMW’]

How to check if an item exists in the list ( Python)

Here we can determine whether an item exists in the list or not as shown below

Example 9:-

carlist = ["maruti","volvo","BMW","Toyota"] 
if "BMW" in carlist:
print("yes. BMW is in the list")

Output:
yes. BMW is in the list

Change item value in a list (Python)

Here, we can change the value of an item in the list by using an index number of the particular item as shown below

Example 10:-

carlist = ["maruti","volvo","BMW","Toyota"] 
carlist[1] = "Scorpio"
print(carlist)

Output:
[‘maruti’, ‘Scorpio’, ‘BMW’, ‘Toyota’]

Change a range of item values (Python)

Also, we can change a range of item values in a list using a range of index numbers separated by a colon (:) as shown below

Example 11:-

carlist = ["maruti","volvo","BMW","Toyota"] 
carlist[1:3] = ["Scorpio","Bolero"]
print(carlist)

Output:
[‘maruti’, ‘Scorpio’, ‘Bolero’, ‘Toyota’]

Append an item in a list in Python

If you want to append an item in a list at the end position then you can do so by using the append() function as shown below

Example 12:-

carlist = ["maruti","volvo","BMW","Toyota"] carlist.append("Marcetis") 
print(carlist)

Output:
[‘maruti’, ‘volvo’, ‘BMW’, ‘Toyota’, ‘Marcetis’]

Insert an item in a list in Python at a particular index

If you want to insert an item at a particular index then you can do so by putting an index number inside the insert() function.

Example 13:-

carlist = ["maruti","volvo","BMW","Toyota"] carlist.insert(1,"Marcetis") 
print(carlist)

Output:
[‘maruti’, ‘Marcetis’, ‘volvo’, ‘BMW’, ‘Toyota’]

Now, you can see that the index number of ‘volvo’ is 1 before, after inserting ‘marcetis’, it goes to index number ‘2’

Extend List in Python

If you want to merge items of two different lists into a single list then you can do so with the help of extend() function.

Example14:-

carlist = ["maruti","volvo","BMW","Toyota"] 
bykelist = ["Honda","Bazaz","TVS"]
carlist.extend(bykelist)
print(carlist)

Output:
[‘maruti’, ‘volvo’, ‘BMW’, ‘Toyota’, ‘Honda’, ‘Bazaz’, ‘TVS’]

Remove items from a list in Python

If you want to remove an item from the list then you can do so by the remove() function as shown below

Example 15:-

carlist = ["maruti","volvo","BMW","Toyota"] 
carlist.remove("volvo")
print(carlist)

Output:
[‘maruti’, ‘BMW’, ‘Toyota’]

Remove a specific item from a list in Python

If you want to remove a specific item from the list then you can do so by using the particular index number inside the pop() function as shown below

Example 16:-

carlist = ["maruti","volvo","BMW","Toyota"] 
carlist.pop(2)
print(carlist)

Output:
[‘maruti’, ‘volvo’, ‘Toyota’]

Without passing the index number inside the pop() function will remove the last item from the list by default.

Remove a specific item from the list using the del keyword

If you want to delete an item from the list using the del keyword then you can do so as shown below

Example 17:-

carlist = ["maruti","volvo","BMW","Toyota"] 
del carlist[1]
print(carlist)

Output:
[‘maruti’, ‘BMW’, ‘Toyota’]

Empty the items in a list in Python

Example 18:-

carlist = [“maruti”,”volvo”,”BMW”,”Toyota”]
carlist.clear()
print(carlist)

Output:
[]

List Comprehension in Python

Sometimes we want to create a new list from an existing list based on multiple conditions then it requires multiple lines of code to be written. In order to display one line of code, we use list comprehension by combining those multiple lines of code. it has the following form

newlist = [ expression for item in iterable if condition == True]

Below is an example to print all the items of a list using for loop and extract those items based on the presence of a particular alphabet such as ‘o’.

Example 19:-

carlist = ["maruti","volvo","BMW","Toyota"] 
newcarlist = []
for x in carlist:
if 'o' in x:
newcarlist.append(x)
print(newcarlist)

You can write the above code using List comprehension as shown below

newcarlist = [x for x in carlist if 'o' in x] 
print(newcarlist)

Output:
[‘volvo’, ‘Toyota’]

Sorting list in ascending order in Python

You can sort the items in a list using the sort() function in Python as shown below

Example 20:-

carlist = ["maruti","volvo","BMW","Toyota"] 
carlist.sort()
print(carlist)

Output:
[‘BMW’, ‘Toyota’, ‘maruti’, ‘volvo’]

Sorting List in descending order in Python

You can sort the items in a list in descending order in Python as shown below

Example 21:-

carlist = ["maruti","volvo","BMW","Toyota"] 
carlist.sort(reverse = True)
print(carlist)

Output:
[‘volvo’, ‘maruti’, ‘Toyota’, ‘BMW’]

Copy list in Python

We can copy the items of an existing list and display all the items in a new list as shown below

Example 22:-

carlist = ["maruti","volvo","BMW","Toyota"] 
newcarlist = carlist.copy()
print(newcarlist)

Output:
[‘maruti’, ‘volvo’, ‘BMW’, ‘Toyota’]

You can download the python latest version from here

Conclusion:- I hope this tutorial will help you to understand the concept. If there is any doubt then please leave a comment below

Originally published at https://pbphpsolutions.com on August 3, 2022.

--

--

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
Bipsmedium

Bipsmedium

25 Followers

Hi, This is Biplab and I am web developer well versed in PHP and other open source technologies. I am here to share my experience with the community.