Python Read Specific Line From a List
Python Lists and List Manipulation
Before starting, I should mention that the code in this weblog post and in the video above is available on my github.
Defining a List
Lists are written inside square brackets []
# Define a list
z = [iii, 7, 4, 2]
Lists store an ordered collection of items which can be of different types. The list defined above has items that are all of the aforementioned blazon (int), only all the items of a list practice non demand to exist of the same type every bit y'all can come across below.
# Define a list
heterogenousElements = [three, Truthful, 'Michael', 2.0]
The listing contains an int, a bool, a cord, and a float.
Access Values in a List
Each item i northward a listing has an assigned index value. It is of import to note that python is a cypher indexed based language. All this ways is that the first item in the list is at alphabetize 0.
# Define a list
z = [three, seven, 4, two] # Admission the first detail of a listing at index 0
print(z[0])
Python also supports negative indexing. Negative indexing starts from the end. It can be more convienient at times to use negative indexing to become the concluding item in the list because you don't have to know the length of the list to access the last item.
# print terminal item in the list
print(z[-one])
As a reminder, you could as well access the same item using positive indexes (every bit seen below).
Slice of Lists
Slices are good for getting a subset of values in your list. For the instance code below, it will return a list with the items from alphabetize 0 upwardly to and not including index 2.
# Ascertain a listing
z = [3, 7, four, 2] print(z[0:2])
# everything upward to simply not including index 3
impress(z[:iii])
The code below returns a list with items from index ane to the end of the list
# index i to end of list
print(z[i:])
Update Particular in a Listing
Lists in Python are mutable. All that means is that later defining a list, it is possible to update the private items in a list.
# Defining a listing
z = [3, vii, 4, 2] # Update the item at index 1 with the cord "fish"
z[one] = "fish"
print(z)
List Methods
Python lists accept different methods that assist you modify a list. This section of the tutorial just goes over various python list methods.
Index Method
# Define a list
z = [4, 1, 5, 4, 10, 4]
The index method returns the first index at which a value occurs. In the lawmaking below, it will return 0.
print(z.alphabetize(four))
You tin also specify where you lot start your search.
print(z.index(4, 3))
Count Method
The count method works but like how it sounds. It counts the number of times a value occurs in a list
random_list = [4, i, five, 4, 10, 4]
random_list.count(5)
Sort Method
The sort method sorts and alters the original list in place.
z = [three, vii, four, 2]
z.sort()
print(z)
The code to a higher place sorts a list from low to loftier. The lawmaking below shows that you can as well sort a list from high to low.
# Sorting and Altering original list
# high to low
z.sort(reverse = True)
print(z)
Every bit an bated, I should mention that you lot can as well sort a listing of strings from a-z and z-a as yous tin can see here.
Append Method
The append method adds an chemical element to the end of a list. This happens inplace.
z = [7, 4, 3, two]
z.append(3)
print(z)
Remove Method
The remove method removes the first occurrence of a value in a listing.
z = [vii, 4, 3, 2, 3]
z.remove(2)
print(z)
Pop Method
The pop method removes an item at the alphabetize you provide. This method will also return the item yous removed from the list. If you don't provide an alphabetize, information technology will by default remove the item at the last alphabetize.
z = [vii, iv, 3, 3]
print(z.popular(1))
impress(z)
Extend Method
The method extends a list by appending items. The benefit of this is you can add lists together.
z = [seven, three, 3]
z.extend([4,5])
print(z)
Alternatively, the same thing could be accomplished by using the + operator.
print([1,2] + [3,4])
Insert Method
The insert method inserts an item before the index you lot provide
z = [vii, 3, iii, 4, 5]
z.insert(4, [1, two])
print(z)
Endmost Remarks
Delight let me know if you have any questions either here or in the comments department of the youtube video or through Twitter! Adjacent postal service reviews python dictionaries. If you want to learn how to utilize the Pandas, Matplotlib, or Seaborn libraries, please consider taking my Python for Data Visualization LinkedIn Learning grade. Here is a free preview video.
Source: https://towardsdatascience.com/python-basics-6-lists-and-list-manipulation-a56be62b1f95
0 Response to "Python Read Specific Line From a List"
Enregistrer un commentaire