Skip to content

[Python] Tutorial(10) List, Array of Python

Last Updated on 2021-04-10 by Clay

"List", is a most commonly used data type in Python. Of course, if you are learning Machine Learning. (The most commonly used data type probably is Numpy.)

If you are used to reading tutorial of Python official, maybe you can refer to this website.

This is the fifth chapter of the official teaching article. The part will not too far with today range.


List

The most commonly used in Python, as mentioned above. List can add numbers, strings, tuple ...... most of the things you will use, most of them can be stored in the List, so that you can make subsequent calls.

The building ways of List is mostly as follows:

  1. Directly set the data you want to set into List directly in List:
List = [1, 2, 3, 'Today', 'is', 'nice.']


2. Save other data (such as tuples) as a for-loop in List:

data = (1, 2, 3, 'Today', 'is', 'nice.')

List = []
for value in data:
    List.append(value)


First assume that we have a set of data —— there is the "data" that exists in the data type of the tuple —— and then we have created an empty array List.

The next step is for-loop.

We stored the values in the tuple in the empty List with append instructions.

print(List)


We print List:

[1, 2, 3, 'Today', 'is', 'nice.']

Yes! the data in the tuple has been saved in List!

In the case, in addition to the most white way of writing a lot of code, there are actually quite a few ways to stored the value in List. (I still use tuple for example, but you can try as many data type as possible.)

a = [n for n in data]
b = list(data)


a. is a common method of establishing values in loops within an array.

b. is a (possible) bad habit, it directly converts the value of tuple into a list!

print(a)
print(b)


Output:

[1, 2, 3, 'Today', 'is', 'nice.']
[1, 2, 3, 'Today', 'is', 'nice.'] 

It seems that no mistakes have occurred.


Remove

List is a "remove" instructions that can removes the item you want to remove.

As a reminder, if the same items exist,it is very likely that only the last item can be removed.

List = [1, 1, 1, 2, 2, 2, 3, 3]
List.remove(1)
print(List)


So what will we remove?

[1, 1, 2, 2, 2, 3, 3]

The answer is: the last one.


Index

The index in List makes it easy for us to find the "value" we want, in which position.

List = [1, 2, 3, 4, 5, 6]
print(List.index(3))
print(List.index(6))


Output:

2
5

(Tips: Indices of List calculation starts from 0! It should be said that most computer language are designed this way!)

Of course, we can also call the value in List with index.

print(List[2])
print(List[5])


Output:

3
6

Insert

The instruction "Insert" requires two input values: (position, value)

For example:

List.insert(0, 100)
print(List)


Output:

[100, 1, 2, 3, 4, 5, 6]

We can see, we inserted the number 100 in the index 0 place!

So, what if we specify that the inserted position is much greater than the actual owned index?

List.insert(10000, 100)
print(List)


Output:

[100, 1, 2, 3, 4, 5, 6, 100]

The value we want to insert will be placed in the last item of List !


List has more functions, and also use it to complete a lot of data structures, but limited by today's space, may only be introduced to this, I hope to bring you a little bit of List.

As more and more Python tutorial are written, more and more realize I'm a not logical person, and often have divergent thinking.

Perhaps when the whole tutorial comes to an end, we will review it from the beginning and fill in some incomplete parts. I hope everyone will continue to support it.

Thanks!


References


Read More

Leave a ReplyCancel reply

Exit mobile version