Python List

Python List



What are lists?

A list store a series of items in a particular order. Lists allows you to store sets of information in one place, whether you have just a few items or millions of items . Lists are one of python's most powerful features readily accessible to new programmers, and they tie together many important concepts in progamming.

Defining a List

Use square brackets to define a list, and use commas to separate individual items in the list. Use plural names for lists, to make your code easier to read.

#Making a list
users = ['val', 'bob', 'mia', 'ron', 'ned']




python list


Accessing elements

Individual elements in a list are accessed according to their position, called the index. The index of the first element is 0, the index of the second element is 1, and so forth. Negative indices refer to items at the end of the list. To get a particular element, write the name of the list and then the index of the element in square brackets.

#Getting the first element
first_user = users[0]
#Getting the second element
second_user = users[1]
#Getting the last element
newest_user = users[-1]



python accessing element


Modifying individual items

Once you've defined a list, you can change individual elements in a list. You do this by referring to the index of the item you want to modify.

#changing an element
users[0] = 'valerie'
users[-2] = 'ronald'




python modifying individual items



Adding elements

You can add elements to the end of a list, or you can insert them wherever you like in a list.
#Adding an element to the end of the list
users.append('amy')
#starting with an empty list
users = []
users.append('val')
users.append('bob')
users.append('mia')
#Inserting elements at a particular position
users.insert(0, 'joe')
users.insert(3, 'bea')




python adding elements



Removing Elements

you can remove elements by their positions in a list, or by the value of the item. If you remove an item by its value. Python removes only the first item that has that value.

#Deleting an element by its  position
del users[-1]
#Removing an item by its value
users.remove('mia')




python removing elements



Popping elements

If you want to work with an element that you're removing from the list, you can "pop" the element. If you think of the list as a stack of items. Pop() takes an item off the top of the stack. By default pop() returns the last element in the list, but you can also pop elements from any position in the list.

#Pop the last items from a list
most_recent_user = users.pop()
print(most_recent_user)
#Pop the first item in a list
first_user = users.pop(0)
print(first_user)




python popping elements



List length

The len() function returns the number of items in a list.

#Find the length of a list
num_users = len(users)
print("we have " + str(num_users) + "users.")





List length




Sorting a list

The sort() method changes the order of a list permanently. The sorted() function returns a copy of the list, leaving the original list unchanged. You can sort the items in a list in alphabetical order, or reverse alphabetical order. You can also reverse the original order of the list. Keep in mind that lowercase and uppercase letters may affect the sort order.

#Sorting a list permanently
users.sort()
#sorting a list permanently in reverse alphabetical order
users.sort(reverse=True)
#sorting a list temporarily
print(sorted(users))
print(sorted(users, reverse=True))
#Reversing the order of a list
users.reverse()



python sorting list



Looping through a list

List can contain millions of item, so python provides an efficient way to loop through all the items in alist. When you set up a loop, python pulls each item from the list one at a time and stores it in a temporary variable, which you provide a name for. This name should be the singular version of the list name.

#Printing all items in a list
for user in users:
print(user)
#Printing a message for each items, and a separate message afterwards
for user in users:
print("Welcome, " + user + "!")
	print("Welcome, we're glad to see you all!")



python looping list

0 comments:

Post a Comment