Mẹo về List dictionary Python Chi Tiết
Pro đang tìm kiếm từ khóa List dictionary Python được Cập Nhật vào lúc : 2022-02-05 19:53:05 . Với phương châm chia sẻ Bí kíp Hướng dẫn trong nội dung bài viết một cách Chi Tiết Mới Nhất. Nếu sau khi tìm hiểu thêm nội dung bài viết vẫn ko hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Tác giả lý giải và hướng dẫn lại nha.
EngEd Community
Sections Engineering Education (EngEd) Program fosters a community of university students in Computer Science related fields of study to research and write about topics that are relevant to engineers in the modern technology landscape. You can find more information and program guidelines in the GitHub repository. If you’re currently enrolled in a Computer Science related field of study and are interested in participating in the program, please complete this form .
Nội dung chính
- EngEd CommunityWorking with Lists and Dictionaries in PythonPrerequisitesCreating a listAccessing elements in a listChanging elements in a listAdding elements to a listRemoving elements from a listSorting a listSlicing a listLooping through a listDictionariesAccessing values in a dictionariesAdding items to a dictionaryChanging values in a dictionaryRemoving key:value pairs from dictionariesLooping through a dictionaryVideo liên quan
Working with Lists and Dictionaries in Python
May 28, 2022
- Topics:Languages
Lists and dictionaries are one of the most often used data structures in Python for data storage and arrangement. This tutorial will explain how to carry out access, modify operations, etc., on lists and dictionaries.
Prerequisites
To follow along with this tutorial, you will require some knowledge of Python.
Creating a list
Lists enable you to store a set of items in place. Lists take the form of arrays in other languages like Java.
A list in Python is created using the following syntax:
listname = [item1, item2, item3, …]
The following is an example of a list of cars:
cars = [‘Volvo’, ‘Subaru’, ‘Skyline’, ‘Ford’]
print(cars) # [‘Volvo’, ‘Subaru’, ‘Skyline’, ‘Ford’]
Accessing elements in a list
You can access elements in a list using the square bracket notation. Indices begin 0, not 1, hence the first item in a list takes index 0, not 1.
The syntax for accessing elements in a list is shown below.
listname = [item1,item2,item3]
print(listname[indexvalue])
The following code requests for the cars index 0 and 3:
cars = [‘Volvo’, ‘Subaru’, ‘Skyline’, ‘Ford’]
print(cars[0]) # Volvo
print(cars[3]) # Ford
Python has a unique way of getting the last item in a list. If you print an item index -1, Python gives back the last item in the list. Indices -2,-3, and -4 gives the second last, third last, and fourth last item respectively.
cars = [‘Volvo’, ‘Subaru’, ‘Skyline’, ‘Ford’]
print(cars[-1]) # Ford
Changing elements in a list
To alter an item in the list, we can assign a new value using square bracket notation.
For instance, in our list of cars, the second item is Subaru. Lets change the value to Suzuki.
cars = [‘Volvo’, ‘Subaru’, ‘Skyline’, ‘Ford’]
print(cars) # [‘Volvo’, ‘Subaru’, ‘Skyline’, ‘Ford’]
cars[1]=’suzuki’
print(cars) # [‘Volvo’, ‘Suzuki’, ‘Skyline’, ‘Ford’]
Adding elements to a list
To add new elements the end of the list, use the .append() method.
For example, lets add Audi to our list of cars.
cars = [‘Volvo’, ‘Subaru’, ‘Skyline’, ‘Ford’]
cars.append(‘Audi’) # adds ‘Audi’ the end of the list
print(cars) # [‘Volvo’, ‘Subaru’, ‘Skyline’, ‘Ford’, ‘Audi’]
You can also use the .insert() method to add a new item any position in your list, by specifying the position and value of the new item.
Lets take a look this example:
cars = [‘Volvo’, ‘Subaru’, ‘Skyline’, ‘Ford’]
cars.insert(2,’Audi’) # adds ‘Audi’ the beginning of the list ( index 2)
print(cars) # [‘Volvo’, ‘Subaru’, ‘Audi’, ‘Skyline’, ‘Ford’]
All the other values in the list are shifted to the right.
Removing elements from a list
If the location of an item that you want to do away with is well known, you can use the del statement.
Example:
cars = [‘Volvo’, ‘Subaru’, ‘Skyline’, ‘Ford’]
del cars[0]
print(cars) # [‘Subaru’, ‘Skyline’, ‘Ford’]
The first car, i.e. Volvo is removed from the list.
If the value of the item you want to remove is known, use the .remove() method.
To illustrate this, lets remove the item Subaru from our list of cars.
cars = [‘Volvo’, ‘Subaru’, ‘Skyline’, ‘Ford’]
cars.remove(‘Subaru’) # removes Subaru from the list
print(cars) # [‘Volvo’, ‘Skyline’, ‘Ford’]
NOTE: When a value appears more than once in a list, the .remove() method only deletes the first occurrence of the value.
Sorting a list
Sorting is organizing your list by some basis, e.g. reverse order, ascending order, or alphabetical order.
To sort a list, use the .sort() method. To understand this better, lets arrange our list of cars alphabetically.
cars = [‘Volvo’, ‘Subaru’, ‘Skyline’, ‘Ford’]
cars.sort() # arranges the list alphabetically
print(cars) # [‘Ford’, ‘Skyline’, ‘Subaru’, ‘Volvo’]
The .reverse() method is used to reverse the order of the list. For instance, lets print our list of cars in reverse.
cars = [‘Volvo’, ‘Subaru’, ‘Skyline’, ‘Ford’]
cars.reverse()
print(cars) # [‘Ford’, ‘Skyline’, ‘Subaru’, ‘Volvo’]
NOTE: .reverse() doesnt sort alphabetically backward. It just reverses the order of the list.
Slicing a list
To derive a segment, you need to specify the index of the first element and where to stop slicing.
In Python, we use a colon(:) as a slicing operator. Slicing follows the following syntax.
list_name[Index_start:Index_end]
For example, when you request [0:3], Python returns the items index 0, 1, and 2.
To illustrate this, lets take a look the following code.
cars = [‘Volvo’, ‘Subaru’, ‘Skyline’, ‘Ford’, ‘Isuzu’]
print(cars[0:3]) # [‘Volvo’, ‘Subaru’, ‘Skyline’]
When you exclude the starting index in a slice, Python automatically begins the slice the start of the list. Similarly, if you omit the last index, Python returns all elements in the list from the first one.
cars = [‘Volvo’, ‘Subaru’, ‘Skyline’, ‘Ford’, ‘Isuzu’]
print(cars[:3]) # [‘Volvo’, ‘Subaru’, ‘Skyline’]
print(cars[3:]) # [‘Ford’, ‘Isuzu’]
Looping through a list
To perform an action to each element in a list, we can use the for loop.
For instance, lets assume we have a list of cars and we want to pull out each name in the cars list.
cars = [‘Volvo’, ‘Subaru’, ‘Skyline’, ‘Ford’, ‘Isuzu’] # initiate the list
for car in cars:
print(car)
# Volvo
# Subaru
# Skyline
# Ford
# Isuzu
The line: for car in cars, tells Python to pull a car from the cars list and save it in the variable car. The line: print (car), then prints the name that was stored in the variable car. This line is re-executed for each item.
Dictionaries
In Python, a dictionary is a group of key:value pairs where each key is linked to a value. A dictionary is enclosed with curly brackets , with a series of key:value pairs inside.
A dictionary follows the following syntax:
dict_name=
key:value,
key:value
Example:
stock_1=
‘type’:’Volvo’,
‘color’:’Black’
print(stock_1) # ‘type’: ‘Volvo’, ‘color’: ‘Black’
Accessing values in a dictionaries
You can access values using the square brackets notation. To get a value, place its related key inside the square brackets as shown below.
stock_1=
‘type’:’Volvo’,
‘color’:’Black’
print(stock_1[‘type’]) # Volvo
Adding items to a dictionary
To add a new item into a dictionary, give a value to a new key. Lets add a new key: year to our stock_1 dictionary, and assign it the value 2014.
stock_1=
‘type’:’Volvo’,
‘color’:’Black’
print(stock_1) # prints the initial dictionary
stock_1[‘year’]=2014 # Adding a new key:value
print(stock_1) # returns the modified dictionary
Changing values in a dictionary
You can change a value in a dictionary by referring to its key name.
For example, lets change the value of the year in our stock_1 dictionary from 2014 to 2022.
stock_1=
‘type’:’Volvo’,
‘color’:’Black’,
‘year’:2014
stock_1[‘year’]=2022 # change the value of year from 2014 to 2022
print(stock_1) # ‘type’: ‘Volvo’, ‘color’: ‘Black’, ‘year’: 2022
Removing key:value pairs from dictionaries
To remove a key:value pair you can use the del statement. Give the key youre trying to remove and the name of the dictionary to the del statement. For instance, lets remove the key color in our stock_1 dictionary.
stock_1=
‘type’:’Volvo’,
‘color’:’Black’,
‘year’:2014
del stock_1[‘color’] # removes the key `color` and its value `Black`
print(stock_1) # ‘type’: ‘Volvo’, ‘year’: 2014
Python removes the key color and its related value Black. The rest of the dictionary remains intact.
Looping through a dictionary
To access everything stored in a dictionary, you can loop through the dictionary using a for loop.
Assuming we have the names and models stored in a cars dictionary, we can access the name and model of each car using a for loop as follows:
cars=
‘Volvo’:’V90′,
‘Skyline’:’R32′,
‘Subaru’:’2019WRX’
for name, model in cars.items(): # method `item()` returns a list of key value pairs
print(name, “:”, model)
# Volvo : V90
# Skyline : R32
# Subaru : 2019WRX
In the example above, the for loop stores the key in the variable name and its value in the variable model.
Conclusion
In this tutorial, we have gained an understanding of lists and dictionaries on how to:
- Access, alter and remove elements in both lists and dictionaries.Loop through dictionaries and lists.Sort and slice a list.
Happy coding!
Peer Review Contributions by: Geoffrey Mungai
Reply
9
0
Chia sẻ
Video List dictionary Python ?
Bạn vừa đọc nội dung bài viết Với Một số hướng dẫn một cách rõ ràng hơn về Review List dictionary Python tiên tiến và phát triển nhất
Chia Sẻ Link Cập nhật List dictionary Python miễn phí
Pro đang tìm một số trong những Chia Sẻ Link Down List dictionary Python Free.
Hỏi đáp vướng mắc về List dictionary Python
Nếu You sau khi đọc nội dung bài viết List dictionary Python , bạn vẫn chưa hiểu thì hoàn toàn có thể lại phản hồi ở cuối bài để Admin lý giải và hướng dẫn lại nha
#List #dictionary #Python