= [1, "me", 3.456, "you", 50]
alist print("The list is alist = {}".format(alist))
The list is alist = [1, 'me', 3.456, 'you', 50]
A list is a simple container object that can hold an arbitrary number of Python objects. A list can be a list of numbers, words, or a combination. Here is an example:
Alternatively we could translate the list into a string and then print it. You can tag together strings with the plus sign, which combines a string "The list is alist = "
together with another string str(alist)
. The Python function str() translates a number into a string, i.e., a word and then prints the combined "word".
We can access the elements of a list bit-by-bit using list indexation. Note that the first element in the list is at position 0.
Python is zero-indexed. This means that the first element in a list (or other collection object such as tuple or array, more on these later) is at position “zero”.
R, on the other hand, is one-indexed. This means that the first element in a list is at position “one”.
Most general programming languages such as C, Java, Ruby, PHP, Python etc. are zero indexed, whereas most applied programming “languages” such as R, Matlab, Julia or Stata are one-indexed.
There is a philosophical debate about zero vs. one indexing going on, but the gist is that most computer scientists strongly prefer zero indexing whereas most math adjacent fields such as statistics and economics prefer one-based indexing as it seems more “natural.” See Wikipedia on Zero Based Indexing
print(alist[0])
print(alist[1])
print(alist[2])
print(alist[3])
print(alist[4])
1
me
3.456
you
50
Or prettier
print("alist[0] = {}".format(alist[0]))
print("alist[1] = {}".format(alist[1]))
print("alist[2] = {}".format(alist[2]))
print("alist[3] = {}".format(alist[3]))
print("alist[4] = {}".format(alist[4]))
alist[0] = 1
alist[1] = me
alist[2] = 3.456
alist[3] = you
alist[4] = 50
If you want to extract more than one element of a list you can use a slice operator. This basically involves the colon symbol :
at the appropriate position. If you want to extract the first three elements you can simply index the list as alist[:3]
. If you want to get everything from element 2 onwards you can alist[2:]
print(alist)
print("alist[0:4] = {}".format(alist[0:4]))
print("alist[:3] = {}".format(alist[:3]))
print("alist[2:] = {}".format(alist[2:]))
[1, 'me', 3.456, 'you', 50]
alist[0:4] = [1, 'me', 3.456, 'you']
alist[:3] = [1, 'me', 3.456]
alist[2:] = [3.456, 'you', 50]
This alist
is already a vector, or atomic vector, and hence you can use cat()
to print it to the screen.
If you want to grab the last element of the list you can start indexing with negative numbers.
= [1, "me", 3.456, "you", 50]
alist print(alist)
print("alist[-1] = {}".format(alist[-1]))
print("alist[-2] = {}".format(alist[-2]))
print("alist[-3] = {}".format(alist[-3]))
[1, 'me', 3.456, 'you', 50]
alist[-1] = 50
alist[-2] = you
alist[-3] = 3.456
alist <- list(1, "me", 3.456, "you", 50)
print(alist)
str(alist)
# or unlist first
my_atomic_vec = unlist(alist)
cat(my_atomic_vec)
print(paste("alist[length(alist)] =", alist[length(alist)]))
cat('\n')
print(paste("alist[length(alist) - 1] =", alist[length(alist) - 1]))
cat('\n')
print(paste("alist[length(alist) - 2] =", alist[length(alist) - 2]))
cat('\n')
[[1]]
[1] 1
[[2]]
[1] "me"
[[3]]
[1] 3.456
[[4]]
[1] "you"
[[5]]
[1] 50
List of 5
$ : num 1
$ : chr "me"
$ : num 3.46
$ : chr "you"
$ : num 50
1 me 3.456 you 50[1] "alist[length(alist)] = 50"
[1] "alist[length(alist) - 1] = you"
[1] "alist[length(alist) - 2] = 3.456"
You can change elements of a list by reassigning them using their index. So if you want to replace the third element of the list with the word "Mom" you simply assign it as
2] = "Mom"
alist[print(alist)
[1, 'me', 'Mom', 'you', 50]
alist[2] = "Mom"
# Again: Don't use cat() here
print(alist)
[[1]]
[1] 1
[[2]]
[1] "Mom"
[[3]]
[1] 3.456
[[4]]
[1] "you"
[[5]]
[1] 50
What about more complicated lists, where the elements inside the list are lists themselves. In this case we are dealing with nested lists. Here is an example:
= [['Mom', 42], ['Dad', 41], ['Kids', 10, 12]]
myNestedList print(myNestedList)
[['Mom', 42], ['Dad', 41], ['Kids', 10, 12]]
A quick not on the R implementation: Nested lists are not directly supported in R as a built-in data structure. However, you can achieve a similar structure using lists and named lists in R.
Now let's see what happens if we index this list. Try the following:
print(myNestedList[0])
print('---------------')
print(myNestedList[1])
print('---------------')
['Mom', 42]
---------------
['Dad', 41]
---------------
Then try
print(myNestedList[0][0])
print('---------------')
print(myNestedList[1][0])
print('---------------')
print(myNestedList[1][1])
print('---------------')
Mom
---------------
Dad
---------------
41
---------------
print(myNestedList[1][1])
print('---------------')
print(myNestedList[2][1])
print('---------------')
print(myNestedList[2][2])
print('---------------')
[[1]]
[[1]]$name
[1] "Mom"
[[1]]$age
[1] 42
[1] "---------------"
[[1]]
[[1]]$name
[1] "Dad"
[[1]]$age
[1] 41
[1] "---------------"
[[1]]
NULL
[1] "---------------"
Now let's go one step deeper into the list:
print(myNestedList[1][0][0])
print('---------------')
print(myNestedList[1][0][1])
print('---------------')
print(myNestedList[1][0][2])
print('---------------')
D
---------------
a
---------------
d
---------------
This example shows how you can extract content from a list inside of a list by simply adding brackets with indexing positions to the list name. If you go outside the range of the inside list, the interpreter will throw an error:
print(myNestedList[1][0][3])
Error: IndexError: string index out of range
print(myNestedList[2][1][4])
[[1]]
NULL
Are immutable lists, that once defined, cannot be changed anymore. It is a read-only list.
= (1, "me", 3.456, "you", 50)
a_tuple print(a_tuple)
(1, 'me', 3.456, 'you', 50)
In order to use tuples in R you first need to install the library sets
with install.packages("sets")
. Then you need to import the library using library(sets)
. Now you are ready to use tuples.
Also, in R you first have to “push” the elements of your tuple into a list and then assign the list as a tuple!
library(sets)
= tuple((1, "me", 3.456, "you", 50))
a_tuple print(a_tuple)
Error: <text>:3:19: unexpected ','
2:
3: a_tuple = tuple((1,
^
Now try to change an element of the tuple and see what happens.
2] = "Mom"
a_tuple[print(a_tuple)
Error: TypeError: 'tuple' object does not support item assignment
(1, 'me', 3.456, 'you', 50)
a_tuple[3] = "Mom"
Error: object 'a_tuple' not found
print(a_tuple)
Error in eval(expr, envir, enclos): object 'a_tuple' not found
Slice operators work exactly the same way as they work on lists.
Dictionaries or short "dicts" are more general mappings and word list associative arrays or so called hashes. They are basically key-value pairs where a key can be almost any Python type. So instead of indexing a list with indexes (which are numbers starting from 0, 1, etc.) the indices of a dictionary can be words or other data types.
Here is a brief example where we use names of people as keys and store various information together with those keys. We can then retrieve the information of each person with the persons name.
# Defining a dictionary as adict = {'name': income}
= {'James': 20000}
adict 'Jim'] = 50000
adict['Tom'] = 80000 adict[
# Defining a dictionary as adict = {'name': income}
adict = {'James': 20000}
Warning: NAs introduced by coercion
Error in "James":20000: NA/NaN argument
adict['Jim'] = 50000
Error: object 'adict' not found
adict['Tom'] = 80000
Error: object 'adict' not found
We can now retrive this info using the key.
print(adict)
print(adict['James'])
print(adict['Jim'])
print(adict['Tom'])
{'James': 20000, 'Jim': 50000, 'Tom': 80000}
20000
50000
80000
print(adict)
Error in eval(expr, envir, enclos): object 'adict' not found
print(adict['James'])
Error in eval(expr, envir, enclos): object 'adict' not found
print(adict['Jim'])
Error in eval(expr, envir, enclos): object 'adict' not found
print(adict['Tom'])
Error in eval(expr, envir, enclos): object 'adict' not found
You can delete an element with
del adict['James']
print(adict)
{'Jim': 50000, 'Tom': 80000}
'James']
del adict[print(adict)
Error: <text>:1:5: unexpected symbol
1: del adict
^
We next give a couple examples of potential pitfalls that can cause programming mistakes.
= [ 1,2,3,4 ]
list1 = list1
list2 5)
list1.append(print(list1)
print(list2)
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
= [ 1,2,3,4 ]
list1 = list1
list2 list1.append(5)
print(list1)
print(list2)
Error: <text>:1:9: unexpected '['
1: list1 = [
^
What happens here is that the list [1,2,3,4]
is assigned to two separate names list1
and list2
. These two names now point to the same, identical list. As soon as you change the list using list1.append(5)
this change will be reflected in both names of the list. If you really just want to copy the list and give that separate copy the name list2
, do the following:
from copy import deepcopy
= [ 1,2,3,4 ]
list1 = deepcopy(list1)
list2 5)
list1.append(print(list1)
print(list2)
[1, 2, 3, 4, 5]
[1, 2, 3, 4]
# In R, you can use 'list()' to create lists, and 'c()' to create vectors.
list1 <- list(1, 2, 3, 4)
list2 <- list1 # This makes a copy of the reference, similar to deepcopy in Python
list1 <- c(list1, 5) # Append 5 to the list1 vector
print(list1)
print(list2)
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] 4
[[5]]
[1] 5
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] 4
sort()
and reverse()
do not return a list objectBut if you now try:
you get an error. So here the sort()
method is not returning a list object. Since we then try to add a list [-1, 0]
to something that is NOT a list, the interpreter throws an error. Here is one way to fix this, use the sorted
command:
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write and can be used to store and communicate information to other products.
It is based on key:value pairs. Many programming languages support the JSON data format. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
In brief, JSON is a way by which we store and exchange data, which is accomplished through its syntax, and is used in many web applications. The nice thing about JSON is that it has a human readable format, and this may be one of the reasons for using it in data transmission, in addition to its effectiveness when working with APIs (Application Programming Interface).
An example of JSON-formatted data is as follows:
{"name": "Frank", "age": 39, "isEmployed": true}
Python has a built in JSON library called json
that needs to be imported if you want to convert a JSON string into a Python value object like a dictionary or a list.
The JSON string has been converted to a dictionary. You can now use it as such.
print(jsonToPython['name'])
Frank
print(jsonToR['name'])
$name
[1] "Frank"
If you want to convert a Python dictionary into a JSON string that can then be written to a file and read by other programs you can use the json.dumps()
function.
import json
= {'name':'Bob', 'age':44, 'isEmployed':True}
pythonDictionary = json.dumps(pythonDictionary)
dictionaryToJson
print(dictionaryToJson)
{"name": "Bob", "age": 44, "isEmployed": true}