• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Rowell Dionicio

Get Techie With It

  • Home
  • About
  • Resources
    • Archives
    • Book List
    • YouTube
  • Learn
    • DevNet Associate
    • PCNSA Certified
  • Blog
  • Contact
  • Show Search
Hide Search

Python Day 2: Lists

September 10, 2018 By Rowell Leave a Comment

Lists look like they’ll be useful when I get to a point where I can write scripts. Python Lists can hold multiple values in an ordered sequence.

Within the list are items which are ordered by an index. The index starts with 0. Each item is separated by a comma and any strings must have quotes around them. 

To assign a list to a variable and retrieve an item from the list the syntax looks like:

[python]
groceries = ['oranges', 'spinach', 'eggs', 'milk']
groceries[0]
'oranges'
[/python]

As I said earlier, the first value in the list starts with 0. It’s possible to go in reverse by using negative indexes:

[python]
groceries = ['oranges', 'spinach', 'eggs', 'milk']
groceries[-1]
'milk'
[/python]

What if you wanted to access multiple values within a list? That’s done by slicing. A slice starts with the first integer, a colon, and the second integer is where the slice ends, but not including that item. Take a look:

[python]
groceries[0:3]
['oranges', 'spinach', 'eggs']
[/python]

Modifying items within a list is possible. Let’s say you wanted to change oranges to apples in the groceries list. We need to modify index 0 within the list.

[python]
groceries[0]
'oranges'
groceries[0] = 'apples'
groceries[0]
'apples'
[/python]

Removing items from a Python list is fairly simple as well by using the del statement. We must reference the index of the item we want to remove.:

[python]
groceries
['apples', 'spinach', 'eggs', 'milk']
del groceries[0]
groceries
['spinach', 'eggs', 'milk']
[/python]

The items in the list move up in their index values which means spinach is now at index 0.

Adding items to the Python list is done by appending it using what’s called a method. Methods are a little different when it comes to lists because we call on the method on a value:

[python]
groceries
['spinach', 'eggs', 'milk']
groceries.append('yogurt')
groceries
['spinach', 'eggs', 'milk', 'yogurt']
[/python]

By appending to the list, the new item we are adding gets added to the end of the list. 

It is possible to add items to the list at a specific index. The index value must be passed, with the new item, using the insert method:

[python]
groceries
['spinach', 'eggs', 'milk', 'yogurt']
groceries.insert(0, 'pears')
groceries
['pears', 'spinach', 'eggs', 'milk', 'yogurt']
[/python]

There’s quite a lot you can do with lists. They are very flexible and can be modified with ease. That’s all I’ve done with Day 2 of my Python learning exercise. The next Day will include more Python List work. 

On to more practicing…

Share this:

  • Facebook
  • LinkedIn
  • Twitter

Related

Filed Under: Coding Tagged With: lists, python

About Rowell

Wi-Fi expert. Coffee addict ☕️. Tech nerd. Business owner.

Reader Interactions

Leave a Reply Cancel reply

Primary Sidebar

Recent Posts

  • Passed Palo Alto Networks Certified Security Administrator (PCNSA)
  • 5 Years Running
  • Q4 2021 and Yearly Income Report
  • I PASSED JNCIA-MistAI
  • Admins and Role-Based Access Control – PCNSA

Categories

  • bschool
  • Certifications
  • Coding
  • DevNet Associate
  • Events
  • Lab
  • Networking
  • Personal
  • Podcasting
  • Professional
  • Reviews
  • Security
  • Short Stories
  • Uncategorized
  • Wireless

Archives

  • May 2022
  • January 2022
  • December 2021
  • November 2021
  • August 2021
  • July 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • November 2018
  • September 2018
  • August 2018

Copyright © 2022 · Written by Rowell Dionicio · You're awesome.