Sunday, December 1, 2019

Python tutorial 13 | List functions

Moving on to the next tutorial in Python Tutorials for Beginners. This Python beginner tutorial is the next part of the Python tutorial 12 | Lists. This tutorial explains many Python list functions with examples. Please view the Python video tutorial 12 and Python video tutorial 13. Or read on...

Let us learn about list functions in Python programming with code examples. The Python list functions are explained as comments (starting with the # symbol in the Python code below). These list functions work on Python lists with different data types (integers, floats or strings), as appropriate.

# Python code examples
# create a list of strings
vowels = ['a', 'e', 'i', 'o', 'u']

# len function returns the number of items in a Python list
# print 5, which is the number of items in the vowels list
print (len(vowels))

# min function returns the smallest item in a Python list
# print 'a', which is the smallest item in the list
print (min(vowels))

# max function returns the largest item in a Python list
# print 'u', which is the largest item in the list
print (max(vowels))

# index list function returns the index of a specific item in a list; indexes start from 0
# print 1, which is the index of item, 'e' in the list
print (vowels.index('e'))

# count list function returns the number of occurrences of a specific item in a list
# print 1, which is the number of times 'i' appears in the list
print (vowels.count('i'))


# insert list function adds a specific item to a list at a specific index
# insert the item 'i' at the index of 2
vowels.insert (2, 'i')
# print ['a', 'e', 'i', 'i', 'o', 'u']
print (vowels)

# append list function adds a specific item to the end of a list
vowels.append ('e')
# print ['a', 'e', 'i', 'i', 'o', 'u', 'e']
print (vowels)

# sort list function sorts the items within a list, meaning the items are put in ascending order
vowels.sort()
# print ['a', 'e', 'e', 'i', 'i', 'o', 'u']
print (vowels)

# reverse list function reverses a list
vowels.reverse()
# print ['u', 'o', 'i', 'i', 'e', 'e', 'a']
print (vowels)

# remove list function removes the first occurrence of a specific item in a list
vowels.remove ('o')
# if the remove function does not find the specific item, it gives an error
# print ['u', 'i', 'i', 'e', 'e', 'a']
print (vowels)

# pop list function works in a couple of ways; if no index is given, it removes the last item of a list
vowels.pop()
# print ['u', 'i', 'i', 'e', 'e']
print (vowels)
# Note: if an index is given, the pop list function removes the item at that index

# extend list function appends all items of another list to a list
vowels.extend (['i', 'i', 'i'])
# print ['u', 'i', 'i', 'e', 'e', 'i', 'i', 'i']
print (vowels)

# clear list function removes all items from a list
vowels.clear()
# print []
print (vowels)

Want to see Python samples of list functions working on a numeric list? And learn about nested lists. Then view my Python video tutorial 13. Thank you.

5 comments:

  1. Hi,
    I would like get some Test Approach for DataBase Migration (Same DB) and the Risks we might face in migration

    ReplyDelete
    Replies
    1. Hi, Sure. You can view my blog post and video on DB migration for the test approach and risks/ challenges therein. The link is http://inderpsingh.blogspot.com/2010/03/how-to-do-database-migration-testing.html

      Delete
  2. Great Post! Thanks for sharing. Keep sharing such information.
    If you want to build and improve your skills in Python, then join our online python training classes in Gurgaon. Python is a strong, flexible, and easy-to-use programming language, our trainers provide you the practical knowledge of python, and train you as per current industry standards. We are the most recommended python training institute in Gurgaon, and also provide 100% job placement support to every candidate who joins our institute. For more details about our python training in Gurgaon, you can call us directly on 70-70-90-50-90

    ReplyDelete