book
  • README
  • cheatsheet
    • bash
      • builtin
      • syntactic sugar
      • cmd
      • havefun
    • text-processing
      • awk
      • sed
      • html
      • json
      • regex
      • unicode
    • osx
    • curl
    • tricky
    • widget
    • proxy
    • colors
    • math
    • media
    • ssl
      • keystore
      • verification
      • server
      • client
      • tricky
    • windows
      • powershell
      • choco
      • wsl
      • wt
      • shortcut
      • clsid
      • env
      • shell:folder
  • vim
    • nvim
    • install
    • color
    • plugins
      • usage
      • other plugins
      • deprecated
    • tricky
    • viml
    • windows
    • troubleshooting
  • devops
    • admin tools
    • ssh
    • git
      • config
      • alias
      • submodule
      • eol
      • example
      • gerrit
        • gerrit API
      • github
      • troubleshooting
      • tricky
      • statistics
    • pre-commit
    • release-tools
    • tmux
      • cheatsheet
    • ansible
    • vault
    • artifactory
      • api
      • cli
      • aql
      • nginx cert
    • klocwork
      • kwadmin
      • kwserver
      • api
      • q&a
    • elk
    • mongodb
    • android
    • mobile
  • jenkins
    • config
      • windows
    • appearance
    • troubleshooting
    • jenkinsfile
      • utility
      • parallel
      • build
      • envvar
      • properties
      • trigger
      • node
    • script
      • job
      • build
      • stage
      • agent
      • security & authorization
      • exception
      • monitor
      • tricky
    • api
      • blueocean
    • cli
    • plugins
      • kubernetes
      • docker
      • shared-libs
      • lockable-resource
      • ansicolor
      • badge
      • groovy-postbuild
      • simple-theme
      • customizable-header
      • artifactory
      • jira-steps
      • job-dsl
      • build-timeline
      • crumbIssuer
      • coverage
      • uno-choice
      • tricky
  • virtualization
    • kubernetes
      • init
        • kubespray
        • kubeadm
          • environment
          • crio v1.30.4
          • docker v1.15.3
          • HA
        • addons
        • etcd
      • kubectl
        • pod
        • deploy
        • replicasets
        • namespace
        • secrets
      • node
      • certificates
      • events
      • kubeconfig
      • kubelet
      • troubleshooting
      • cheatsheet
      • auth
      • api
      • tools
        • monitor
        • helm
        • network
        • minikube
    • docker
      • run & exec
      • voume
      • remove
      • show info
      • dockerfile
      • dockerd
      • tricky
      • troubleshooting
      • windows
    • crio
    • podman
  • ai
    • prompt
  • osx
    • apps
      • init
      • brew
    • defaults
    • system
    • network
    • script
    • tricky
  • linux
    • devenv
    • util
      • time & date
      • output formatting
      • params
      • tricky
    • nutshell
    • disk
    • network
    • troubleshooting
    • system
      • apt/yum/snap
      • authorization
      • apps
      • x11
    • ubuntu
      • systemctl
      • x
    • rpi
  • programming
    • groovy
    • python
      • config
      • basic
      • list
      • pip
      • q&a
    • others
    • archive
      • angular
      • maven
      • mysql
        • installation
        • logs
      • ruby
        • rubyInstallationQ&A
  • tools
    • fonts
    • html & css
    • Jira & Confluence
    • node & npm
      • gitbook
      • hexo
      • github.page
      • code themes
    • app
      • microsoft office
      • vscode
      • virtualbox
      • iterm2
      • browser
      • skype
      • teamviewer
      • others
  • quotes
  • english
Powered by GitBook
On this page

Was this helpful?

  1. programming
  2. python

list

PreviousbasicNextpip

Last updated 1 year ago

Was this helpful?

list copy

inspired by in stackoverflow

clone_list = sample_list.copy()
  • or

    clone_list = sample_list[:]
  • or

    clone_list = list(sample_list)
  • or

    import copy
    clone_list = copy.copy(sample_list)
  • or

    import copy
    clone_list = copy.deepcopy(sample_list)
  • example:

    >>> id(x)
    4505979072
    >>> k = x
    >>> id(k)
    4505979072
    
    >>> k = x.copy()
    >>> id(k)
    4445208000
    >>> k = x[:]
    >>> id(k)
    4505977632
    >>> import copy
    >>> k = copy.copy(x)
    >>> id(k)
    4505754352
    >>> k = copy.deepcopy(x)
    >>> id(k)
    4505978352
    >>> k = x[:]
    >>> id(k)
    4506260896
    >>> k = copy.deepcopy(x)
    >>> id(k)
    4506261136

reverse list

sample_list[::-1]
  • example:

    >>> ['1', '2', '3', '4', '5'][::-1]
    ['5', '4', '3', '2', '1']

cast format (str -> int)

list( map(int, sample_list) )
  • example:

    >>> print( list( map(int, ['2', '8', '4', '127', 'HKD'][:3][::-1] ) ) )
    [4, 8, 2]

zip two lists

>> from itertools import zip_longest
>>> x = ['1', '2', '3', '4']
>>> y = ['one', 'two', 'three', 'four']
>>> for i, j in zip_longest( x, y ):
  print(i, j)

1 one
2 two
3 three
4 four
  • or zip to a map

    >>> x = ['1', '2', '3', '4']
    >>> y = ['one', 'two', 'three', 'four']
    >>> print( {key: value for key, value in zip_longest(x, y)} )
    {'1': 'one', '2': 'two', '3': 'three', '4': 'four'}

list mathematical

  • sum

    >>> n = ['1', '2', '3', '4']
    >>> print( sum( list( map(int, n) ) ) )
    10
  • multiplication

How to clone or copy a list?
list copy
reverse list
cast format (str -> int)
zip two lists
list mathematical