All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Append To A List In Python

Last Updated : Mar 11, 2024

How To Append To A List In Python

In this article we will show you the solution of how to append to a list in python, in python, there are several methods to append a list in python such as append() method, or using concatenation(‘+’) method, extend method.

These methods can add an elements or items to the end of list or you can also append a list in python using loop.

Step By Step Guide On How To Append To A List In Python :-

Method 1 - Using append() method to append a list in python

list1= [2,4,5]
list1.append(6)
print(list1) #[2,4,5,6]
  1. In this method, we use append method to append a list in python.
  2. In this code, we create a list named list1. We can append a new element in the list which is present at the end of list or create a list containing strings, or we can append a new items in the list.
  3. Hence, the output of this code:- [2,4,5,6]

Method 2 - Using concatenation(‘+’) method to append a list in python

list1= [2,4,5, ‘hello’]
list2= [6]
mylist= list1 + list2
print(mylist) #[2,4,5,’hello’,6]
  1. In this method, we ‘+’ operator to concatenate a lists , this method will also help to append a list in python.
  2. In this code, we create a two lists named list1 and list2.list 1 contains some numeric value or strings, list2 contain numeric value, so we can concatenate both lists using ‘+’ operator and store in mylist. You can also concatenate multiple lists using ‘+’ operator. You can also concatenate first list2 then list1
  3. Hence, the output of this code- [2,4,5,’hello’, 6]

Method 3 - Using extend method to append a list in python

List1= [2,4,5]
List2=[6,7,8]
mylist=List1. extend(List2)
print(mylist) #[2,4,5,6,7,8]
  1. In this method, we use extend method to append a list in python.
  2. In this code, we create a two lists named- List1, List2. The extend method adds list2 to list1. You can also extend first List 2 then List1.
  3. Hence, the output of this code:- [2,4,5,6,7,8]

Method 4 - Using loop to append a list in python

l1= [1,2,3]
l2=[4,5,6]
for i in l2:
    l1=l1+[i]
  print(l1) #[1,2,3,4,5,6]
  1. In this method, Using loop for appending a list in python
  2. In this code we create a two list named as l1 and l2. After that we can iterating each element in l2.
  3. We concatenate l1 and [i].
  4. Hence, the output of this code:- [1,2,3,4,5,6]

Conclusion :-

In conclusion, we easily say that append a list in python is an easy task.

We can use different- different methods to append a list in python. Methods including append(), extend(), ‘+’ operator or we can use loop to append a list in python.

Where ‘+’ operator concatenate a list in python but append() method is an easy method, it gives the updated list while concatenate (‘+’) method is used when multiple lists is given, they give the output in the single list.

I hope this article on how to append to a list in python helps you and the steps and method mentioned above are easy to follow and implement.