Do lists start at 1 in Python?

Lists are the most commonly used data structure. A list is as a sequence of data that is enclosed in square brackets and separated by a comma. Individual data pieces can be accessed by calling its index value.

3 Built in List Functions

To find the length of the list or the number of elements in a list, len[ ] is used.

If the list consists of all integer elements then min[ ] and max[ ] gives the minimum and maximum value in the list. Similarly sum is the sum

num = [1,2,3,4,5,6,7,8,9] print["min =",min[num]," max =",max[num]," total =",sum[num]]
min = 1 max = 9 total = 45

Lists can be concatenated by adding, + them. The resultant list will contain all the elements of the lists that were added. The resultant list will not be a nested list.

There might arise a requirement where you need to check if a particular element is there in a predefined list. Consider the below list.

names = ['Earth','Air','Fire','Water']

To check if Fire and Space is present in the list names, a conventional approach would be to use a for loop and iterate over the list and use the if condition. But in python you can use a in b concept which would return True if a is present in b and False if not.

In a list with string elements, max[ ] and min[ ] are still applicable and return the first/last element in lexicographical order.

mlist = ['bzaa', 'acs', 'ac', 'az', 'zg', 'k'] print["max =",max[mlist]] print["min =",min[mlist]]

When comparing ASCII values, the system proceeds character by character, starting with the first character. If there is exactly one element whose first character has the lowest / highest ASCII value, min/max outputs this element. In the above example, this is the case for the maximum string zg. For the minimum string there are several elements with an a at the first place, so the min function compares the second places here. With acs and ac there are two possible candidates for the minimal string. If a string stops, it is automatically smaller than a string that continues, i.e. ac is smaller than acs. This procedure is the same as the alphabetical sorting.

However, if you write numbers as strings and want to output the minimum or maximum, they are also compared character by character. Thereby it is ignored how many characters the number contains, i.e. whether it is written in tens, hundreds, thousands, range. This explains strange errors, as the following example shows:

nlist = ['5', '10', '93', '94', '1000'] print["max =",max[nlist]] print['min =',min[nlist]]

If you want to find the max[ ] string element based on the length of the string then another parameter key can be used to specify the function to use for generating the value on which to sort. Hence finding the longest and shortest string in mlist can be done using the len function:

print['longest =',max[mlist, key=len]] print['shortest =',min[mlist, key=len]]
longest = bzaa shortest = k

Any other built-in or user defined function can be used.

A string can be converted into a list by using the list[] function, or more usefully using the split[] method, which breaks strings up based on spaces.

print[list['hello world !'],'Hello World !!'.split[]]
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', ' ', '!'] ['Hello', 'World', '!!']

append[ ] is used to add a single element at the end of the list.

lst = [1,1,4,8,7] lst.append[1] lst

Appending multiple elements to a list would create a sublist. To avoid a nested list then the extend[ ] function can be used.

lst.extend[[10,11,12]] lst
[1, 1, 4, 8, 7, 1, 10, 11, 12]

count[ ] is used to count the number of a particular element that is present in the list.

index[ ] is used to find the index value of a particular element. Note that if there are multiple elements of the same value then the first index value of that element is returned.

lst.index[1], lst.index[11]

insert[x,y] is used to insert a element y at a specified index value x. append[ ] function made it only possible to insert at the end.

[1, 1, 4, 8, 7, 'name', 1, 10, 11, 12]

insert[x,y] inserts but does not replace elements. If you want to replace an element with another element you simply assign the value to that particular index.

[1, 1, 4, 8, 7, 'Python', 1, 10, 11, 12]

pop[ ] removes and returns the last element in the list.

The Index value can be specified to pop a ceratin element corresponding to that index value.

pop[ ] is used to remove an element based on its index value. One can also remove element by specifying the element itself using the remove[ ] function.

Alternative to remove function but with using index value is del. It is basically the same as pop without returning it.

The entire elements present in the list can be reversed by using the reverse[] function.

Note that in case of a nested list an element like [5,4,2,8] is treated as a single element of the parent list lst. Thus the elements inside the nested list is not reversed.

lst2 = [['a','b'], [5,4,2,8], [1], []] lst2.reverse[] lst2
[[], [1], [5, 4, 2, 8], ['a', 'b']]

Python offers built in operation sort[ ] to arrange the elements in ascending order. Alternatively sorted[] can be used to construct a copy of the list in sorted order without changing the original list.

For descending order use the parameter reverse and set it to True.

lst.sort[reverse=True] lst

Similarly for lists containing string elements, sort[ ] would sort the elements based on its ASCII value in ascending and by specifying reverse=True in descending.

['Air', 'Earth', 'Fire', 'Water']
names.sort[reverse=True] names
['Water', 'Fire', 'Earth', 'Air']

To sort based on length key=len should be specified as shown.

['Air', 'Fire', 'Water', 'Earth']
print[sorted[names,key=len,reverse=True]]
['Water', 'Earth', 'Fire', 'Air']

Video liên quan

Chủ Đề