Python Read Specific Line From a List

Python Lists and List Manipulation

Michael Galarnyk

Python Lists and List Manipulation Video

Defining a List

Defining a List. The second row in this tabular array index is how you access items in the list.
          # Define a list
z = [iii, 7, 4, 2]
          # Define a list
heterogenousElements = [three, Truthful, 'Michael', 2.0]

Access Values in a List

Access item at index 0 (in blue)
          # Define a list
z = [three, seven, 4, two]
# Admission the first detail of a listing at index 0
print(z[0])

Output of accessing the item at index 0.

Accessing the item at the last index.
                      # print terminal item in the list            
print(z[-one])

Output of accessing the last detail in the List

Alternative way of accessing the last item in the list z

Slice of Lists

Get-go index is inclusive (before the :) and final (after the :) is not
          # Ascertain a listing
z = [3, 7, four, 2]
print(z[0:2])

Slice of a listing syntax.

                      # everything upward to simply not including index 3            
impress(z[:iii])

                      # index i to end of list            
print(z[i:])

Update Particular in a Listing

          # Defining a listing
z = [3, vii, 4, 2]
# Update the item at index 1 with the cord "fish"
z[one] = "fish"
print(z)

Code to modify an particular in a list

List Methods

Index Method

          # Define a list
z = [4, 1, 5, 4, 10, 4]

          print(z.alphabetize(four))        

          print(z.index(4, 3))        

Count Method

          random_list = [4, i, five, 4, 10, 4]
random_list.count(5)

Sort Method

Sort a Python List - the bodily code would be: z.sort()
          z = [three, vii, four, 2]
z.sort()
print(z)

Sort a python list from loftier to depression
                      # Sorting and Altering original list            
# high to low
z.sort(reverse = True)
print(z)

Append Method

Add the value 3 to the end of the list.
          z = [7, 4, 3, two]
z.append(3)
print(z)

Remove Method

          z = [vii, 4, 3, 2, 3]
z.remove(2)
print(z)

Code removes the beginning occurrence of the value 2 from the list z

Pop Method

z.pop(1) removes the value at index one and returns the value 4.
          z = [vii, iv, 3, 3]
print(z.popular(1))
impress(z)

Extend Method

          z = [seven, three, 3]
z.extend([4,5])
print(z)

Add together the list [4, 5] to the finish of the list z.
          print([1,2] + [3,4])        

Insert Method

insert the list [1,2] at index 4
          z = [vii, 3, iii, 4, 5]
z.insert(4, [1, two])
print(z)

Endmost Remarks

blackmalactind.blogspot.com

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel