All labs · lesson 5 of 9

R vs Pythonintermediate20 min

R/Python Comparison: Data Structures

Explore lists, vectors, and dictionaries in R and Python

Edit any code block and press Run (or Ctrl/⌘ + Enter, or Shift + Enter). The code runs in your browser, so there is nothing to install. The first run downloads the language once; later runs are instant.

Understand how to work with collections of data in both languages.

Lists/Vectors Basics

Both languages have ways to store multiple values.

Exercise 1

Create and manipulate lists/vectors

# Lists in Python
numbers = [1, 2, 3, 4, 5]
fruits = ['apple', 'banana', 'cherry']
mixed = [1, 'hello', 3.14, True]

print(f"Numbers: {numbers}")
print(f"First number: {numbers[0]}")
print(f"Last number: {numbers[-1]}")
print(f"Length: {len(numbers)}")
# Vectors in R
numbers = c(1, 2, 3, 4, 5)
fruits = c('apple', 'banana', 'cherry')
# Note: R vectors are homogeneous

print(paste("Numbers:", paste(numbers, collapse=', ')))
print(paste("First number:", numbers[1]))
print(paste("Last number:", numbers[length(numbers)]))
print(paste("Length:", length(numbers)))

List Operations

Adding, removing, and modifying elements.

Exercise 2

Perform common list operations

# List operations in Python
fruits = ['apple', 'banana']

# Add elements
fruits.append('cherry')
print(f"After append: {fruits}")

fruits.insert(1, 'orange')
print(f"After insert: {fruits}")

# Remove elements
fruits.remove('banana')
print(f"After remove: {fruits}")

# Check if element exists
if 'apple' in fruits:
    print("Apple is in the list!")
# Vector operations in R
fruits = c('apple', 'banana')

# Add elements
fruits = c(fruits, 'cherry')
print(paste("After append:", paste(fruits, collapse=', ')))

fruits = append(fruits, 'orange', after=1)
print(paste("After insert:", paste(fruits, collapse=', ')))

# Remove elements (by index)
fruits = fruits[fruits != 'banana']
print(paste("After remove:", paste(fruits, collapse=', ')))

# Check if element exists
if ('apple' %in% fruits) {
    print("Apple is in the vector!")
}

Dictionaries/Named Lists

Store key-value pairs.

Exercise 3

Work with dictionaries/named lists

# Dictionary in Python
person = {
    'name': 'Alice',
    'age': 30,
    'city': 'New York'
}

print(f"Name: {person['name']}")
print(f"Age: {person['age']}")

# Add new key
person['email'] = 'alice@example.com'

# Get all keys and values
print(f"Keys: {list(person.keys())}")
print(f"Values: {list(person.values())}")
# Named list in R
person = list(
    name = 'Alice',
    age = 30,
    city = 'New York'
)

print(paste("Name:", person$name))
print(paste("Age:", person$age))

# Add new element
person$email = 'alice@example.com'

# Get all names
print(paste("Names:", paste(names(person), collapse=', ')))

List Comprehensions/Apply

Create new lists based on existing ones.

Exercise 4

Transform lists efficiently

# List comprehension in Python
numbers = [1, 2, 3, 4, 5]

# Square each number
squares = [x**2 for x in numbers]
print(f"Squares: {squares}")

# Filter even numbers
evens = [x for x in numbers if x % 2 == 0]
print(f"Even numbers: {evens}")

# Double and filter
doubled_evens = [x*2 for x in numbers if x % 2 == 0]
print(f"Doubled evens: {doubled_evens}")
# sapply/lapply in R
numbers = c(1, 2, 3, 4, 5)

# Square each number
squares = sapply(numbers, function(x) x^2)
print(paste("Squares:", paste(squares, collapse=', ')))

# Filter even numbers
evens = numbers[numbers %% 2 == 0]
print(paste("Even numbers:", paste(evens, collapse=', ')))

# Double and filter
doubled_evens = sapply(evens, function(x) x*2)
print(paste("Doubled evens:", paste(doubled_evens, collapse=', ')))

Key Differences

  • Python lists are 0-indexed, R vectors are 1-indexed
  • Python uses negative indices for reverse indexing, R doesn’t
  • Python dictionaries use {}, R uses named lists with list()
  • Python has list comprehensions [x for x in list]
  • R uses sapply(), lapply(), apply() family
  • R vectors are homogeneous (same type), Python lists can be mixed