Hide / show the code
= c(1,2,3, 'batman', 'wonder woman', 'winnie the pooh')
aprint(a)
[1] "1" "2" "3" "batman"
[5] "wonder woman" "winnie the pooh"
Burak Demirtas
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! š
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:
[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:
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!
Instead of 1 for example, we could have put ābatmanā.
In R
, to create a list, we need to use list()
and it will have similar properties like a Python
list:
$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:
[1] 1 2 3 4
[1] 10 20 30 40 50 60 70 80 90 100
Some other examples of lists in both R and Python as below:
{'a': [1, 2, 3, 4], 'b': [10, 20, 30, 40, 50, 60, 70, 80, 90, 100], 'c': ['a', 'b', 'c']}
1
[1] 1
[1] 2
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!