There is a basic data structure in Python called a tuple, but few people use it because lists are pretty much what we need during development.

Even so, you should never assume that tuples are redundant. Otherwise you wouldn’t often get asked in an interview, what’s the difference between a tuple and a list? Why tuples?

The following two points, the first point is well known to everyone, and the second point may only be known to old drivers, only by learning the second point can we truly understand the value and significance of the existence of tuples.

1. Immutable lists

This is the most striking feature of tuples that distinguishes them from lists.

  • List: mutable sequence
  • Tuple: Immutable sequence

So what is an immutable sequence?

After a tuple object is generated, functions such as inserting elements, deleting elements, adding elements, emptying elements, and modifying elements in the list are not available in tuples, and you cannot modify them.

Since tuples are immutable, their methods are also limited. Here’s a list.

# s1 and s2 are both tuples s1=(1,2,3) s2=(4,5,6) # splice to generate a new tuple s1+s2 s1.__add__(s2) # whether it contains 2 in s1 s1.__contains__(2) # count the number of times the element contains S1.count (2) # get element s1[0] s1.__getitem__(0) # Find index s1.index(2) # get length len(s1) # repeat concatenation s1*n1234567891011121314151617181920212223242526Copy the code

2. Named tuples

This property, in my opinion, is what tuples are all about.

It might be a little confusing to just talk about named tuples. If you call it a record with a field name, you might get the idea.

Here’s an example, but the implementation with field names requires a library (Collections) support, which you’ll need to import.

From collections import namedtuple # generate a City class City = namedtuple("City", "Name country polulation coordinates") # instantiated Tokyo = City("Tokyo", 'JP', '36.93', (' 35.68 ', '139,69)) print (Tokyo) # City (name =' Tokyo 'country =' JP ', polulation = '36.93', coordinates = (' 35.68 ', '139,69) print (Tokyo. Name) # Tokyo12345678910Copy the code

It looks a bit like a dictionary, doesn’t it, but it’s not a dictionary (and the way you get values is different from a dictionary), and a dictionary is mutable. Once a tuple is created, it cannot be modified. To some extent, this suggests that tuples are suitable for storing data that does not need to be modified. Like up there, place names, countries, latitude and longitude.

In addition to the above usage, here are some of the tuples’ own proprietary attributes.

Print (city._fields) ('name', 'country', 'polulation', 'coordinates') 'lat long') Xiamen_tuple = ('Xiemen', 'China', '40 ', _make(Xiamen_tuple) print(Xiamen) # City(name=' Xiamen_tuple ', country='China', Polulation = ', 40 ', coordinates = (24.26, Xiamen_dict = Xiamen._asdict() print(Xiamen_dict) # OrderedDict([('name', 'Xiemen'), ('country', 'China'), ('polulation', '40,54'), ('coordinates', LatLong(lat=24.26, long=118.03))])123456789101112131415Copy the code

In summary, tuples are a powerful data type that can be used as records, which is why they are valuable and meaningful. Its second role, as it is known, is as an immutable list. (The above is my personal opinion, if you have different opinions, please leave a comment.)

At the end of the article’s welfare

My original “PyCharm Chinese Guide” became popular in Python circle once it was released some time ago. Within one day, the number of downloads exceeded 1000 and hundreds of stars were obtained on Github on the same day. Up to now, the number of downloads has exceeded 10,000.

The book runs to nearly 200 pages, and contains numerous illustrations that are so well produced that every Python engineer deserves a copy.

For your convenience, I have uploaded the book to baidu network disk, you can get it by yourself.

Link: pan.baidu.com/s/1-NzATHFt…

Password: mft3

Alternatively, you can simply add my group, which contains hundreds of Python books, and click on the link to join the group.