Python - List, Set, Tuple讲解

The basic Python data structures in Python include list, set, tuples, and dictionary. Each of the data structures is unique in its own way. Data structure are “containers” that organize and group data according to type.

The data structure differ based on mutability and order.
Mutability refers to the ability to change an object after its creation. Mutable objects can be modified, added, or deleted after they’ve been created.

Immutable objects cannot be modified after their creation.

Order relates to whether the position of an element can be used to access the element.


Lists

A list is defined as an ordered collection of items, and it is one of the essential data structures when using Python to create a project.
The term “ordered collections” means that each item in a list comes with an order that uniquely identifies them. The order of elements is an inherent characteristic that remains constant throughout the life of the list.

Since everything in Python is considered an object, creating a list is essentially creating a Python object of a specific type. When creating a list, all the items in the list should be put in square brackets and separated by commas to let Python know that a list has been created. A sample list can be written as follows:

List_A = [item 1, item 2, item 3, ... , item n]

Lists can be nested

A list can be nested, which means that it can contain any type of object. It can include another list or a sublist - which can subsequently contain other sublists itself. There is no limit to the depth with which lists can be nested. An example of a nested list is as follows:

List_A = [item1, list_B, item 3 ...., item n]

Lists are mutable

Lists created in Python qualify to be mutable because they can be altered even after being created. A user can search, add, shift, move, and delete elements from a list at their own will. When replacing elements in a list, the number of elements added does not need to be equal to the number of elements, and Python will adjust itself as needed.

It also allows you to replace a single element in a list with multiple elements. Mutability also enables the user to input additional elements into the list without making any replacements.


Python Data Structures - Tuples

A tuple is a built-in data structure in Python that is an ordered collection of objects. Unlike lists, tuples come with limited functionality.

Tuples are immutable. Tuples cannot be modified, added, or deleted once they’ve been created. Lists are defined by using parentheses to enclose the elements, which are separated by commas.

The use of parentheses in creating tuples is optional, but they are recommended to distinguish between the start and end of the tuple. A sample tuple is written as follows:

tuple_A = (item 1, item 2, item 3, ... , item n)

Empty and one single item tuple

When writing a tuple with only a single element, the coder must use a comma after the item. This is done to enable Python to differentiate between the tuple and the parentheses surrounding the object in the equation. A tuple with a single item can be expressed as follows:

some_tuple = (item 1, )

If the tuple is empty, the user should include an empty pair of parentheses as follows:

empty_tuple = ()

Why Tuples are Preferred over Lists

Tuples are preferred when the user does not want the data to be modified. Sometimes, the user can create an object that is intended to remain intact during its lifetime. Tuples are immutable, so they can be used to prevent accidental addition, modification, or removal of data.

Also, tuples use less memory, and they make program execution faster than using lists. Lists are slower than tuples because every time a new execution is done with lists, new objects are created, and the objects are not interpreted just once. Tuples are identified by Python as one immutable object. Hence, they are built as one single entity.


Python Data Structures - Sets

A set is defined as a unique collection of unique elements that do not follow a specific order. Sets are used when the existence of an object in a collection of objects is more important than the number of times it appears or the order of the objects. Unlike tuples, sets are mutable - they can be modified, added, replaced, or removed. A sample set can be represented as follows:

set_a = {item 1, item 2, ... , item n}

One of the ways that sets are used is when checking whether or not some elements are contained in a set or not. For example, sets are highly optimized for membership tests. They can be used to check whether a set is subset of another set and to identify the relationship between two sets.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值