Python! Wait for the R guy! - Lists

R
Python
Author

Burak Demirtas

Published

May 23, 2023

Lists seem similar in R and Python in terms of the structure. But creating and using them is a little bit different. Yet , no worries! Easy for a R wizard like you! šŸ˜Ž

Lists

Basic List

When we call lists in R and Python, actually we are not referring the completely same type of objects.

When you create a vector in R, you define it like this:

Hide / show the code
a= c(1,2,3, 'batman', 'wonder woman', 'winnie the pooh')
print(a)
[1] "1"               "2"               "3"               "batman"         
[5] "wonder woman"    "winnie the pooh"

So, when you try to access in any of the elements, you just need to call itā€™s position in the vector:

Hide / show the code
print(a[5])
[1] "wonder woman"

In Python , when you create a vector, actually you are creating a ā€˜listā€™. Thatā€™s why Python people need objects like np.arrays in numpy library for 1 row vectors or a.k.a arrays!

Hide / show the code
b = [1, 2, 3, 4, 5]
print(b)
[1, 2, 3, 4, 5]

Instead of 1 for example, we could have put ā€œbatmanā€.

Hide / show the code
b = ["batman", 2, 3, 4, 5]
print(b)
['batman', 2, 3, 4, 5]
As you see, when we put 1 and batman in the same vector, R turned the 1 to an integer and put like ā€œ1ā€ instead of 1. But Python didnā€™t. Because Python didnā€™t create an array or vector with this.

In R , to create a list, we need to use list() and it will have similar properties like a Python list:

Hide / show the code
simpleListA <- list(
  a = 1:4,
  b = seq(10, 100, by = 10),
  c = c("a", "b", "c")
)
print(simpleListA)
$a
[1] 1 2 3 4

$b
 [1]  10  20  30  40  50  60  70  80  90 100

$c
[1] "a" "b" "c"

As you see, on the print out, it first opens a bracket : [ ] , then puts another bracket inside [ [ ] ]. The purpose of this structure is to allow us call any element in our list with precise addressing like a is the state, 1 is the city in that state:

Hide / show the code
print(simpleListA[[1]])    # Accessing element by integer index: 1
[1] 1 2 3 4
Hide / show the code
print(simpleListA[["b"]])  # Accessing element by name: "b"
 [1]  10  20  30  40  50  60  70  80  90 100

Some other examples of lists in both R and Python as below:

Hide / show the code
simpleListA = {
    "a": list(range(1, 5)),
    "b": list(range(10, 101, 10)),
    "c": ["a", "b", "c"]
}
print(simpleListA)
{'a': [1, 2, 3, 4], 'b': [10, 20, 30, 40, 50, 60, 70, 80, 90, 100], 'c': ['a', 'b', 'c']}
Hide / show the code
# Python list
my_list = [1, 2, 3]
print(my_list[0])      # Accessing element by integer index: 0
1
Hide / show the code
# R list
my_list <- list(a = 1, b = 2, c = 3)
print(my_list[[1]])    # Accessing element by integer index: 1
[1] 1
Hide / show the code
print(my_list[["b"]])  # Accessing element by name: "b"
[1] 2
Hide / show the code
# Python list
my_list = [1, 2, 3]
print(my_list[0])      # Accessing element by integer index: 0
1

Conclusion

Even if there are some syntax differences between 2 languages, I donā€™t see any big deal for a R user to switch on Python side smoothly in terms of lists.

Letā€™s see on other topics what could we do!