Tipping Points
Lists in Python contain any types of data elements
Create a list
majors = ["Cyber", "CS", "EE", 123, "Math", "Econ"]
Note that a Python list does not need to have all elements in the same type.
Can a text be converted into a list? - Yes!
Then, HOW?
Can any other external data sources be formated in a list? For example,
streaming data from yahoo.com/finance? How about data from an Excel file?
Yes, then HOW?
Access a list
element by an index script
What is returned?
elements by the range of index script
What is returned?
elements by a subscript of index
What is returned?
What about the below?
Adding to a list
.append() - Add an element or a list
myMajor.append("Pharm")
print(majors)
.extend() - Extend an element of a list
Difference between append and extend:
Try the following
myMajor.append(['append','append2'])
myMajor.extend(['extend','extend2'])
print(majors)
.insert( , ) - Add an element or a list into a specific position.
The first parament is the index of a position to insert, and the second parameter is either
an element or a list to add.
myMajor.insert(2, "Math")
print(majors)
Another example:
moreMj = ["Music", "Paint"]
myMajor.insert(3, moreMj)
print(majors)
+ Append two lists
longMj = majors + moreMj
print(longMj)
Removing an element from a list
.remove() - remove the given element
myMajor.remove(123)
print(majors)
Note the built-in function del() deletes a specified list.
del(majors[1])
print(majors)
.pop( ) - remove the element at the specified position
myMajor.remove(2)
print(majors)
See how? Click the button "To See More" to go to the right pane!
To See More