Episode #31 | Lists


Wednesday, 14 Aug 2019 Kudzai Nyandoro 13:14 Minutes

Description:

Lists are a mutable collection of randomly typed objects. They do not have a fixed size and we can use certain methods to add or remove items from the collection.

Notes:

counties = ['fresno', 'kern', 'kings', 'madera', 'merced', 'san joaquin', 'stanislaus', 'tulare']

Lists support the same operations as Strings.

# Return the entire list
counties[:]
# Return all except the first item in the list
counties[1:]
# Return the last item in the list
counties[-1]
# Return the item from index number 0
counties[0]
# Return the item from index number 1
counties[1]
# Return the item from index number 2
counties[2]
# Return the item from index number 5
counties[5]
# Return the item from index number 7
counties[7]
# Return the item from index number 0
counties[-1]
# Add an item to the end of the list
counties.append('mariposa')
# Delete the last item in the list
counties.pop()
# Delete the 4th item in the list
counties.pop(3)
# Remove a specific item in the list
counties.remove('madera')
# Print the counties list
counties
# Sort the list
counites.sort()
# Put the list in reverse order
counties.reverse()
# Loop through the list
for county in counties:
    print(county)
# Create a list using the range method
list(range(51)
# Create a list 0 - 50 in 2 step increments
list(range(0, 51, 2)
# Create a list 0 - 50 in 3 increments
list(range(0, 51, 3)
# Create a list 0 - 100 in 10 increments
list(range(0, 101, 10)