Sitemap

Python: Getting Started with Lists

3 min readMay 14, 2020

--

I’m learning Python, and as a revision method I’ve decided to type up all my notes. So just keep that in mind if you’re here looking for answers, I’m not an expert!

The Basics

To create a list, use [ ] and use commas to separate items

List_Example = ['Ana', 'Bobby', 'Tyler', 'Jenna', 'Josh', 'Debby']

It is also possible to create an empty list

Empty_List = [ ]

You can add one value to a list, using .append()

List_Num = [1, 2, 3]List_Num.append(4)

You can combine multiple lists by creating a new one and using +

List_Num = [1, 2, 3]List_Num2 = List_Num + [4, 5, 6]

Another way you can combine two lists, is using zip()

New_List = zip(old_list1, old_list2)

You can also create lists that follow a ranged pattern:

Up_To_Six = range(7)print(Up_To_Six)
>>> 0, 1, 2, 3, 4, 5, 6

The range will stop 1 number before the stated one. (So basically the count will go up to that number) Notice it starts at 0.

You can also start your range on a different number and have it follow a pattern.

Skip_2 = range(2,9,2)print(Skip_2)
>>> 2, 4, 6, 8

This list skips every two numbers, starting at 2 and ending at 9

Working with Lists

We can find the length of a list by using len

Names = ['Ana', 'Bobby', 'Tyler', 'Jenna', 'Josh', 'Debby']print(len(Names))
>>> 6

Python lists are “zero-indexed” — this means the first element in a list has an index of 0. We use index numbers to get information from a list. For example, to find the first or last element in a list:

Names = ['Ana', 'Bobby', 'Tyler', 'Jenna', 'Josh', 'Debby']print(Names(1))
>>> 'Ana'
print(Names(-1))
>>> 'Debby'

Lists can be also be sliced, we can do this by using the following code

List_Name [Start:End]

Start = Index of the first element to be included

Get Ana Richie’s stories in your inbox

Join Medium for free to get updates from this writer.

End = Index of a number higher than the last index we want to include (so if we want to include up to 8, we input 9 and so on)

Names = ['Ana', 'Bobby', 'Tyler', 'Jenna', 'Josh', 'Debby']Numbers = [ 1, 2, 3, 4, 5, 6]print(Names[2:4])
>>> Tyler, Jenna, Josh
print(Numbers[1:3]
>>> 2,3

If you wanted to, for example, select the first 3 elements, you can also do this by using [0:3] or [:3]. We could also select the last few items by using [5:0] or [5:0]. If we wanted to select the last numbers, we can use negative numbers [-3:] — this would select the last 3 items of the list.

Names = ['Ana', 'Bobby', 'Tyler', 'Jenna', 'Josh', 'Debby']Numbers = [ 1, 2, 3, 4, 5, 6]print(Names[:4]) #Start from 0 and end at 3
>>> Ana, Bobby, Tyler
print(Names[4:]) #Start at 4 and end at the end of the list
>>> Josh, Debby
print(Numbers[-3:] #Selects last 3 items in the list
>>> 4, 5, 6

We can use the function .count() to count within a list

Votes = [ 'Tom', 'Tom', 'Matt', 'Mark', 'Travis', 'Tom','Mark', 'Travis', 'Travis', 'Tom','Mark','Matt']Tom_Votes =  Votes.count('Tom')
print(Tom_Votes)
>>> 4
Travis_Votes = Votes.count('Travis')
print(Travis_Votes)
>>> 3

Sorting Lists

There are different ways we can sort a list. The first method is to sort it in place, we do this by using .sort(). This will sort the list ascending by default.

When assigned to a variable, .sort() will not return anything, but it will sort the selected list.

Games = [ 'The Sims' , 'PacMan', 'Pokemon', 'Tetris']Games.sort() print(Games)
>>>['PacMan', 'Pokemon', 'Tetris', 'The Sims']

After sorting the list we need to call to print() to view the result.

To sort “out of place” or — in a new list, we can use sorted

Games = [ 'The Sims' , 'PacMan', 'Pokemon', 'Tetris']print(games_sorted)
>>>['PacMan', 'Pokemon', 'Tetris', 'The Sims']

Notice that .sort() goes after the list name, while sorted goes before.

Press enter or click to view image in full size
Photo by Jefferson Santos on Unsplash

--

--

Ana Richie
Ana Richie

Written by Ana Richie

Just a place to drop my Python Notes 🤪