Creating Lists

A list is a data structure capable of storing different types of data. Run this set of commands in Repl.it:

a=2
b=3
alist_examp=[1,3.4,'asdf',96, True, 9.6,'zxcv',False, 2>5,a+b]
print(alist_examp)
print(type(alist_examp))
print('This list contains ', len(alist_examp), ' elements')

You should see the list output to the screen as well as the data type of the variable alist_examp. Notice that the list contains bool, int, str, and float data. It also contains the result of relational and arithmetic operations. Any valid data type in Python can be placed within a list.

The data contained within the list are called elements. The list you printed contains 10 elements. You can figure this out by counting them by hand. There is also a command called len that will give you this information, which you can see in the example. len is a very useful command.

Pay very close attention to the syntax used to build the list. The left square bracket/right square bracket is known as a "container". Elements must be separated by a comma when building the list.

Pay very close attention to this example. Add this code on the line after print('This list contains ', len(alist_examp), ' elements').

tmp=alist_examp
print(tmp)
print(alist_examp)
tmp[3]='vbnm'
print(tmp)
print(alist_examp)

Notice that changing the value in the variable tmp also changed the value in the variable alist_examp. Initially, this appears to go against what we have learned about the assignment operation (=). For reasons that will be discussed in a later unit, when it comes to lists, Python views both variables as occupying the same space in memory. They are different names that refer to the same data. How then can we truly assign the list to a new variable that occupies a different place in memory and can be modified independently of the variable alist_examp? This set of commands will accomplish this:

tmp=alist_examp.copy()
print(tmp)
print(alist_examp)
tmp[3]='vbnm'
print(tmp)
print(alist_examp)

This example introduces some new syntax that will soon become second nature. The command "alist_examp.copy()" is your first introduction to object-oriented syntax. The "copy()" portion of this command is what is known as a method. It is connected to the variable alist_exmp using the period or 'dot notation'. This command makes a true hard copy of the variable alist_examp so that modifications of tmp do not affect alist_examp.

Every variable type in Python is what is known as an "object". You will learn an immense amount about objects as we delve deeper into the course. The main point to realize is that there are several methods available for use with lists. The copy method we just introduced is one of them. Some other important ones worth mentioning at this point are pop, append, remove, and insert. Pay attention to the sections that describe these methods in detail.

Lists Basics

A container is a construct used to group related values together and contains references to other objects instead of data. 

A list is a container created by surrounding a sequence of variables or literals with brackets [ ]. 

Example:my_list = [10, 'abc',2.5] 

creates a new list variable my_list that contains the three items: 10, 'abc', and 2.5 

A list item is called an element.

A list is also a sequence (just like string), meaning the contained elements are ordered by position in the list, known as the element's index, starting with 0


Example 1:

myList=[1,2,3,"a","hello",8 3 is at position 2

myList[2]
3

my_list = [ ]   creates an empty list


Example 2:

names = ["Jane","Margo","Sally"]

names print all elements of the list names

['Jane', 'Margo', 'Sally']

names.append("Sam")  add another element to the list

names; display the list

['Jane', 'Margo', 'Sally', 'Sam']


Example 3:

myList2=[1,2,3,5]

myList2[2] = 7

myList2

[1,2,7,5]

len(myList2)get the length of the list

4


Example 4:

mylist2: [1, 2, 7, 5]

myList2.pop() remove the last element

5

myList2.pop(1) remove the element with index 

1

myList2
[1,7]


Example 5: 
myList3=[10,9,8,7,6,5,4,6,3,2,1]
myList3.remove(6)
finds and removes first occurrence of value 6 in the list

myList3

[10,9,8,7,5,4,6,3,2,1]

myList3.index(8)

get the position/index of value 8

2

myList3.insert(3,12)

myList3

[10,9,8,12,7,5,4,6,3,2,1]


In-class work

Assume we have two lists:

a = [1,2,3,4] and b = ['a','b','c']

Answer the following questions 

(assume you are working with Python Shell):

1) How to display the last element of the list a?

2) How to remove the last element from the list b?

3) The function call len(a) returns the length of the list a.

Write the command to display the sum of the lengths of lists a and b (using len(), print() and + ).

Still working with lists 

a = [1,2,3,4] and b = ['a','b','c']

Answer the following questions 

(assume you are working with Python Shell):

4) What will happen if you type the following statement:

a+b ?

5) How can we “save” the result of (a+b) so that we could use it sometime later in a program?


Source: Natalia Novak, https://academicworks.cuny.edu/bx_oers/30/
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 License.

Last modified: Wednesday, March 9, 2022, 6:13 PM