Lists, Tuples, Sets

These data types are all used to store multiple items in one variable. However, each data type have different formats and uses.

Javascript: Arrays

Arrays in Javascript are the equivalent to Lists, Tuples, and Sets in Python

const sesame_street = ["Elmo", "Cookie Monster", "Oscar the Grouch", "Grover"] 
let sesame_street = sesame_street[0]

Lists

Lists use square brackets [] and are ordered and can be modfied (are changeable). Lists are best used for storing multiple values of the same or different data types. Items can be duplicated in lists. Lists can be modified (lists are mutable).

sesame = ["Elmo", "Cookie Monster", "Oscar the Grouch", "Grover"]
print(sesame)

# Lists are indexed. "0" = the first item, "1" = the second item, and so on
print(sesame[2])

# %%
#looping this list
for x in sesame:
  print(x)

# %%
sesame = ["Elmo", "Cookie Monster", "Oscar the Grouch", "Grover"]
print(sesame)

sesame[3] = "Big Bird"
print(sesame)

Tuples

Tuples use round brackets () and are ordered and are be defined/unmutable (cannot be changed). Tuples are useful for representing fixed sequences of data. Tuples are indexed so the first item is “0”, the second item is “1”, and so on. Items in tuples can be duplicated.


teletubbies = ("Dipsy", "Tinky-Winky", "Po", "Laa-Laa")
print(teletubbies)



#What happens you try to change the tuple?
teletubbies[2] = "Dipsy"

#The tuple does not allow for the change to occur

Sets

  • Sets use curly brackets {} and are unordered and unmutable (cannot be changed). Sets are best used for tasks such as removing duplicates from a list or performing set operations. Duplicates aren’t allowed in sets but adding and removing items are allowed. Indexing on sets can’t be done because sets are unordered.
# %%
backyardigans = {"Uniqua", "Pablo", "Tyrone", "Tasha"}
print(backyardigans)

# %%
#To add an item in a set:
backyardigans.add("Austin")
print(backyardigans)

#To remove an item in a set do: setname.remove
#Example: backyardigans.remove("Pablo")

Videos for additional information



If you want a more in-depth one: