# Data structures - R vs Python

I’ve learnt python since the beginning of this year. In this blog, I’ll comparethe data structures in R to Python briefly.ArrayRAtomic vectors one-dimensional array contain only one data type sc...

I’ve learnt python since the beginning of this year. In this blog, I’ll compare the data structures in R to Python briefly.

## Array

### R

Atomic vectors

• one-dimensional array
• contain only one data type
• scalars are one-element vectors, e.g. f <- 3, g <- "US"
• function c()

Matrices

• two-dimensional array
• each element has the same mode (numeric, character or logical)
• function matrix()

Arrays

• similar to matrices
• more than two dimensions
• function array()

### Python

• package numpy
• functions numpy.array(), numpy.arange()

## List

### R

• an ordered collection of objects
• allow to gather a variety of objects under one name

### Python

• variable-length
• can be modified in-place
• [], list()
• methods: append(), insert(), pop(), remove(), extend(), sort()

## Dataframe

### R

• data.frame()

### Python

• contain an ordered collection of columns
• have both row and column index
• package pandas
• pandas.DataFrame()

Besides, there are some data structures which don’t exist in both R and Python:

## Factors (R)

• nominal / ordinal / continuous
• factor()

## Tuple (Python)

• fixed-length
• immutable
• tuple()

## Dict (Python)

• hash map, associative array
• key-value pairs
• {}, ,
• methods: del, pop(), update()

## Set (Python)

• unordered collection
• unique element
• set(), {}
• set operations: union, intersection, difference, symmetric difference

## List, Set and Dict comprehensions (Python)

• form a new list by filtering the elements of a collection
• transform the elements passing the filter in one concise expression

• list comprehension [expr for val in collection if condition]

• dict comprehension dict_comp = {key-expr : value-expr for value in collection if condition}

• set comprehension set_comp = {expr for value in collection if condition}