Python list operations are Shared using examples

  • 2020-04-02 13:26:11
  • OfStack


Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> cast=["cleese","palin","jones","idle"]
>>> print(cast)
['cleese', 'palin', 'jones', 'idle']
>>> print(len(cast))# Displays the number of data items 
4
>>> print(cast[1])# Displays the number one in the list 2 The value of the data item 
palin
>>> cast.append("gilliam")# Add a data item to the end of the list 
>>> print(cast)
['cleese', 'palin', 'jones', 'idle', 'gilliam']
>>> cast.pop()# Delete the data item at the end of the list 
'gilliam'
>>> print(cast)
['cleese', 'palin', 'jones', 'idle']
>>> cast.extend(["gilliam","chapman"])# Adds a collection of data items to the end of the list 
>>> print(cast)
['cleese', 'palin', 'jones', 'idle', 'gilliam', 'chapman']
>>> cast.remove("chapman")# Deletes the specified data item 
>>> print(cast)
['cleese', 'palin', 'jones', 'idle', 'gilliam']
>>> cast.insert(0,"chapman")# Adds a data item at the specified location 
>>> print(cast)
['chapman', 'cleese', 'palin', 'jones', 'idle', 'gilliam']
>>>

The following is the use and logic of defining a def function, the isinstance() function, for in, if else, etc


movies=["the holy grail",1975,"terry jone & terry gilliam",91,
       ["graham chapman",
       ["michael palin","john cleese","terry gilliam",
            "eric idle","terry jones"]]]
def print_lol(the_list):# Define a function 
        for each_item in the_list:#for in The loop iterates through the list, from the beginning of the list to the end 
        if isinstance(each_item,list):#isinstance() detection each_item Of each item 
                                              # Isn't it list type 
            print_lol(each_item)# If so, call the function print_lol
        else:print(each_item)# If not, print this term 
print_lol(movies)# in movies The function is called in the list 
"""
 before if else Misalignment of statements results in an error 
"""


Related articles: