Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Python List reverse - DesignLinux

designlinux 0 Comments

When working with lists in Python, you may sometimes need to reverse the elements of a list. Reversing a list means that the first element of the list becomes the last, the second become second-to-last, the last element becomes the first, and so on.

In Python, there are several different ways to reverse a list, depending on what you’re trying to do.

reverse() method #

reverse() is a list data type method that reverses the elements of the list in-place. This method modifies the original list rather than creating a new one.

The syntax of the reverse() method is as follows:

list.reverse()

reverse() doesn’t accept arguments.

Here is an example:

capitals = ['Oslo', 'Skopje', 'Riga', 'Madrid']

capitals.reverse()

print('Reversed list:', capitals)
Reversed list: ['Madrid', 'Riga', 'Skopje', 'Oslo']

reversed() function #

reversed() is a Python built-in function that returns a reversed iterator of a given iterable object. The original list is not modified.

If you only want to iterate over the list’s elements in reverse order prefer using the reversed() function, as it is faster than reversing the elements in place`.

The syntax of the reversed() function is as follows:

reversed(seq) 

Where seq is the list you want to revert.

Below is an example using reversed() to loop over the elements of list in reversed order:

numbers = [1, 2, 3, 4]

for i in reversed(numbers) :
    print(i)
4
3
2
1

If you want to convert the reversed iterator into a list, use the list() constructor:

numbers = [1, 2, 3, 4]

print(list(reversed(numbers)))
[4, 3, 2, 1]

Reverse a List using Slicing #

Slice notation is a Python built-in feature that allows you to extract parts of a sequential data type. Although not very Pythonic, you can use the [::-1] notation reverse a list:

numbers = [1, 2, 3, 4]

print(numbers[::-1])

The result of slicing a list is a new list containing the extracted elements. The original list is not modified.

[4, 3, 2, 1]

Conclusion #

To reverse a Python list in place, use the reverse() method. If you only need to create a reversed iterator, use the reversed() function.

If you have any questions or feedback, feel free to leave a comment.

python

Related

Tags: python

How to Install Memcached on Debian 10

Prev Post

How to Install Postman on Ubuntu 20.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 1
  • 75
  • 605,825

DesignLinux.com © All rights reserved

Go to mobile version