Python Lists

Python Collection (Array)

There are four types of collection data types in the Python programming language:

  • ListIt is an ordered and mutable collection. Duplicate members are allowed.
  • TupleIt is an ordered and immutable collection. Duplicate members are allowed.
  • SetIt is an unordered and unindexed collection. There are no duplicate members.
  • DictionaryIt is an unordered, mutable, and indexed collection. There are no duplicate members.

It is useful to understand the properties of the type when choosing a collection type.

Choosing the correct type for a specific dataset may mean retaining meaning, and it may mean improving efficiency or security.

List

A list is an ordered and mutable collection. In Python, lists are written using square brackets.

Instance

Create a list:

thislist = ["apple", "banana", "cherry"]
print(thislist)

Run Instance

Access item

You can access list items by referencing the index number:

Instance

Print the second item of the list:

thislist = ["apple", "banana", "cherry"]
print(thislist[1])

Run Instance

Negative index

Negative index represents starting from the end, -1 represents the last item, -2 represents the second last item, and so on.

Instance

Print the last item of the list:

thislist = ["apple", "banana", "cherry"]
print(thislist[-1])

Run Instance

Index range

You can specify an index range by specifying the start and end of the range.

After specifying a range, the returned value will be a new list containing the specified items.

Instance

Return the third, fourth, and fifth items:

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])

Run Instance

Note:The search will start from index 2 (inclusive) and end at index 5 (exclusive).

Remember, the index of the first item is 0.

Range of negative index

If you need to search from the end of the list, please specify a negative index:

Instance

This example will return items from index -4 (inclusive) to index -1 (exclusive):

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[-4:-1])

Run Instance

Change item value

If you need to change the value of a specific item, please refer to the index number:

Instance

Change the second item:

thislist = ["apple", "banana", "cherry"]
thislist[1] = "mango"
print(thislist)

Run Instance

to traverse the list

You can use for Loop through list items:

Instance

Print all items in the list one by one:

thislist = ["apple", "banana", "cherry"]
for x in thislist:
  print(x)

Run Instance

You will Python For Loop In this chapter, you will learn about for More about loops.

Check if item exists

If you need to determine if a specified item exists in the list, please use in Keyword:

Instance

Check if 'apple' exists in the list:

thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
  print("Yes, 'apple' is in the fruits list")

Run Instance

List length

If you need to determine how many items are in the list, please use len() Method:

Instance

Print the number of items in the list:

thislist = ["apple", "banana", "cherry"]
print(len(thislist))

Run Instance

Add item

If you need to add an item to the end of the list, please use append() Method:

Instance

Use append() Method to append an item:

thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)

Run Instance

To add an item at a specified index, please use insert() Method:

Instance

Insert an item as the second position:

thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)

Run Instance

Delete item

There are several methods to delete items from a list:

Instance

remove() Method to delete a specified item:

thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)

Run Instance

Instance

pop() Method to delete a specified index (if no index is specified, the last item is deleted):

thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)

Run Instance

Instance

del The keyword can delete a specified index:

thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)

Run Instance

Instance

del The keyword can also completely delete the list:

thislist = ["apple", "banana", "cherry"]
del thislist

Run Instance

Instance

clear() Method to clear the list:

thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)

Run Instance

copy list

You can only copy the list by typing list2 = list1 to copy the list, because:list2 which will just be a copy of list1 reference,list1 Changes made will also be automatically reflected in list2 in the process.

There are several methods for copying, one of which is to use the built-in List method copy().

Instance

Use copy() Method to copy the list:

thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)

Run Instance

Another method to make a copy is to use the built-in method list().

Instance

Use list() Method to copy the list:

thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)

Run Instance

Merge two lists

In Python, there are several methods to concatenate or join two or more lists.

One of the simplest methods is to use the + operator.

Instance

Merge two lists:

list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)

Run Instance

Another way to concatenate two lists is to append all items from list2 one by one to list1:

Instance

Append list2 to list1:

list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
for x in list2:
  list1.append(x)
print(list1)

Run Instance

Alternatively, you can use the extend() method, which is intended to add elements from one list to another:

Instance

Use the extend() method to add list2 to the end of list1:

list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)

Run Instance

the list() constructor function

You can also use list() The constructor function creates a new list.

Instance

Use list() Constructor function creates a list:

thislist = list(("apple", "banana", "cherry")) # Please note the parentheses
print(thislist)

Run Instance

List Methods

Python has a set of built-in methods that can be used on lists.

Method Description
append() Add an Element to the End of the List
clear() Clear All Elements from the List
copy() Return a Copy of the List
count() Return the Number of Elements with Specified Values
extend() Add List Elements (or Any Iterable Elements) to the End of the Current List
index() Return the Index of the First Element with Specified Value
insert() Add Elements at Specified Positions
pop() Delete Elements at Specified Positions
remove() Delete Items with Specified Values
reverse() Reverse the Order of the List
sort() Sort the List