my_list=['Alex','Ronald','John']
my_list.append('Ravi')
print(my_list)
Output is here
['Alex', 'Ronald', 'John', 'Ravi']
my_list=['Alex','Ronald','John']
my_list2=['Ravi','John','Ronald']
my_list.append(my_list2)
print(my_list)
Output is here
['Alex', 'Ronald', 'John', ['Ravi', 'John', 'Ronald']]
In the above code the list is added at the end of my_list. To add only the elements of another list ( any iterable ) we can use extend(). This is the difference between append and extend methods.
my_list=['Alex','Ronald','John']
my_list2=['Ravi','John','Ronald']
my_list.extend(my_list2)
print(my_list)
Output is here
['Alex', 'Ronald', 'John', 'Ravi', 'John', 'Ronald']
my_list=['Alex','Ronald','John']
#my_list.append('new name') # adds at the end of the list
my_list.insert(len(my_list),'new name') # adds at the end of the list
print(my_list)
Using len() to check the length of the list
my_list=['Alex','Ronald','John']
print("Length before append : ",len(my_list))
my_list.append('Ravi')
print("Length after append : ",len(my_list))
print(my_list)
Output is here
Length before append : 3
Length after append : 4
['Alex', 'Ronald', 'John', 'Ravi']
import json
path="D:\\my_data\\student.json" # use your sample file.
fob=open(path,)
data=json.load(fob)
names=[]
for student in data:
#print(student['name'])
names.append(student['name'])
print(names)
fob.close()
All list methods
Questions with solutions on ListAuthor
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.