# 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"               "2"               "3"               "batman"
 "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)``
`` "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 2 3 4

\$b
  10  20  30  40  50  60  70  80  90 100

\$c
 "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[])    # Accessing element by integer index: 1``
`` 1 2 3 4``
Hide / show the code
``print(simpleListA[["b"]])  # Accessing element by name: "b"``
``   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)      # 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[])    # Accessing element by integer index: 1``````
`` 1``
Hide / show the code
``print(my_list[["b"]])  # Accessing element by name: "b"``
`` 2``
Hide / show the code
``````# Python list
my_list = [1, 2, 3]
print(my_list)      # 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!