Sunday, March 29, 2020

IslandT: Lists in python – Python list methods

In this chapter, we will create simple python programs that will demonstrate the usage of various python list methods.

# create a simple python program which will manipulate the content of a python list data type

student_group = []
student_group.append("Rock") # add a student to the group
student_group2 = ["Rick", "Robert", "Robetto"]
student_group.extend(student_group2) # add the list of the second group to the first
print(student_group)
student_group.insert(1, "Ray") # insert Rick at index 1 in the list
print(student_group)
student_group.remove("Robetto") # remove Robetto from the student list
print(student_group)
student_group.sort() # sort the list according to alphabet order
print(student_group)
student_group2.reverse() # reverse the order of the student_group2 list
print(student_group2)
The outcome of the above python list methods program

In the below python list program we will loop through the above python list just like any iterable object.

for name in student_group:
    print(name) # print out the name of each student within the first student group
The names within the first student group

In the next chapter, we will write a few short python programs that will further explore the usage of the python list!



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...