Differences in list, tuple, set and dictionary in Python
List, tuple, set and dictionary are all built-in data types to store the data collections. List, tuple and set store multiple values in a single variable, while the dictionary stores data values in key:value pairs. This article will talk about the differences in the method of creating them, the key features, and the applications.
- List
(1) Creation: square brackets
thislist = ['blue', 'green', 'yellow', 'pink', 'white']
# or use the list constructor
thislist = list(('blue', 'green', 'yellow', 'pink', 'white'))
(2) Features
- Ordered: It means that the items in a list have a defined order, and that order will not change. It also means we could assess the items by indexing. For example,
print(thislist[2]) # returns 'yellow'
print(thislist[:3]) # returns ['blue', 'green', 'yellow']
- Changeable: It means we can change, add, and remove items in a list after it is created. For example,
thislist[0] = 'light blue' # change ‘blue’ to ‘light blue’
thislist.insert(2, 'the third place') # insert the 'the third place' to the third place in the list
thislist.append('black') # append the 'black' at the end of the list
thislist.remove('yellow') # remove the item 'yellow' in the list
- Allow duplicates: Since the list is indexed, it can have items with the same values. For example,
thislist2 = ['blue', 'green', 'yellow', 'blue']
(3) Applications
- Used in Array operations. We could use lists as arrays with built-in commands (NOTE: to work with arrays, you have to import a library like Numpy)
- Used in databases. We could create the database using list. Since it could store different data types, we use list and tuples here to make a database. For example,
thislist3 = [
('name', 'gender', 'subject', 'score'),
('John', 'M', 'Math', 90),
('Emily', 'F', 'Biology', 85),
('Helen', 'F', 'History', 92)]
- Used in JSON format. Convert Python list to JSON string. For example,
import json
thislist4 = [12, 45, 83, 9.5, 33.7, 98]
jsonStr = json.dumps(thislist4)
print(jsonStr) # returns [12, 45, 83, 9.5, 33.7, 98]
2. Tuple
(1) Creation: Use round brackets.
thistuple = ('John', 'M', 'Math', 90)
# or use the tuple constructor
thistuple = tuple(('John', 'M', 'Math', 90))
(2) Features
- Ordered
- Unchangeable: We cannot change, add, or remove items after the tuple is created.
- Allow duplicates
(3) Applications
- Used to insert records in the database through SQL query at a time
3. Set
(1) Creation: use curly brackets
thisset = {'apple', 'banana', 'cherry'}
# or use the tuple constructor
thisset = set(('apple', 'banana', 'cherry'))
(2) Features
- Unordered: It means you cannot refer items by indexes or keys.
- Unchangeable: (Although you cannot change its items, you could remove or add new items)
thisset.add('orange')
thisset.remove('apple')
- Do not allow duplicates: The set cannot contain the same values in one set.
(3) Applications
- Finding unique elements. For example,
thisset2 = {'apple', 'orange', 'apple'}
print(thisset2) # returns { 'apple', 'orange'}
- Join operations. Two or more sets could be joined. For example,
thisset3 = {'grape', 'blueburry'}
thisset.union(thisset3)
# returns {'apple', 'banana', 'cherry', 'grape', 'blueburry'}
4. Dictionary
(1) Creation: use curly brackets with keys and values
thisdict = {
'name': 'John',
'gender': 'M'
'subject': 'Math',
'grade': 90}
(2) Features
- Ordered
- Changeable
- Do not allow duplicates: It cannot contain the same keys.
(3) Applications
- Used to create a dataframe with the list
import pandas as pd
# list of name, gender, subject, and score
nme = ['John', 'Emily', 'Hellen']
gd = ['M', 'F', 'F']
sbj = ['Math', 'Biology', 'History']
scr = [90, 85, 92]
# dictionary of lists
dict = {'name': nme, 'gender': gd, 'subject': sbj, 'score': scr}
df = pd.DataFrame(dict)
df
- Used in JSON