

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How does concatenation operator work on list in Python?
The concatenation operator creates a new list in Python using the initial lists in the order they were added in. This is not an inplace operation.
example
list1 = [1, 2, 3] list2 = ['a', 'b'] list3 = list1 + list2 print(list3)
Output
This will give the output −
[1, 2, 3, 'a', 'b']
There are other ways to concatenate 2 lists. Easiest is to use the extend function, if you want to extend the list in place.
example
list1 = [1, 2, 3] list2 = ['a', 'b'] list1.extend(list2) print(list1)
Output
This will give the output −
[1, 2, 3, 'a', 'b']
You can also use unpacking operator * to create list from 2 lists. This can be used only in Python 3.5+.
Example
list1 = [1, 2, 3] list2 = ['a', 'b'] list3 = [*list1, *list2] print(list3)
Output
This will give the output −
[1, 2, 3, 'a', 'b']
- Related Questions & Answers
- How does concatenation operator work on tuple in Python?
- How does * operator work on list in Python?
- How does in operator work on list in Python?
- How does repetition operator work on list in Python?
- How does del operator work on list in Python?
- How does the * operator work on a tuple in Python?
- How does the repetition operator work on a tuple in Python?
- How does the del operator work on a tuple in Python?
- How does the 'in' operator work on a tuple in Python?
- JavaScript : Why does % operator work on strings? - (Type Coercion)
- How does the Comma Operator work in C++
- MySQL concatenation operator?
- String Concatenation by + (string concatenation) operator.
- How does a list work in Java?
- How does comparison operator work with date values in MySQL?
Advertisements