Collection Combine In Python

Introduction

Python basically has four collections:

  • List
  • Dictionary
  • Set
  • Tuple

This list is mutable, and can be accessed using indexes, It also stores duplicates.

a = [1, 2]
b = [3, 4]
print(a + b) # combine just by adding '+'

#output will be [1,2,3,4]

Dictionary is mutable. It stores values in the format of key:value pair.

a = {1:2}
b = {3:4}
a.update(b) #Combine using update method
print(a)
# output {1: 2, 3: 4}

Set is unordered. It does not store duplicate entries.

a={1,2}
b={3,4}
a.update(b)
print(a)
# output {1, 2, 3, 4}

Tuple is immutable. It stores duplicates.

a = (1, 2)
b = (3, 4)
print(a + b)
#output (1, 2, 3, 4)

Summary

In this article, we learned with simple examples how to combine two collections in Python.


Recommended Free Ebook
Similar Articles