CodeRef
CodeRef
  • Software
    • VSCode
  • Midjourney_AI
    • Midjourney Cheat Sheet
  • aws
    • S3 Reference
    • Services
  • bash
    • .bashrc File Contents
    • CAN
    • _System Config File Locations
    • argument-length-limits
    • Conditionals
    • Data Structures
    • File Permissions
    • File Syncing
    • File System
    • Functions
    • General
    • Loops
    • My Functions
    • Networking
    • Number Operations
    • OpenVPN
    • Operators
    • Resource Management
    • Serial RS232
    • Spinning Wheel Animation
    • SSH
    • Text Operations
    • Environment Variables
  • cpp
    • ChatGPT | Pointers vs. References
    • arrays
    • Classes
    • Data Types / Structures
    • Enumerated Classes
    • Exception Handling
    • Function Objects
    • Functions
    • I/O
    • Loops
    • Macros
    • Namespaces
    • New Features
    • Pointers
    • Scope
    • Smart Pointers
    • Raw String Literals
    • Style Guide
    • Switch Case
    • Templating
    • How to Use tinyxml2
    • Useful Libraries
    • google-test
    • Conditionals
    • Rule of Three/Five
    • Optional Parameters
    • Keywords
    • Filesystem
    • Random
    • Casting
    • tools
  • git
    • Code Review Dependency Strategy
    • Git Bisect Guide
    • Git Reference
    • removing-cherry-picks
    • Useful Tools
    • Graphite Reference
  • js
    • functions
    • Javascript Reference
  • linux
    • Display
    • Dual Boot with Windows
    • File System
    • NVIDIA
    • Sending/Receiving TCP/UDP Messages in Ubuntu
    • dynamically_linked_binaries
  • markdown
    • Images
    • obsidian-reference
  • python
    • Classes
    • Exceptions
    • Functions
    • Operations
    • Python Reference
    • unittest_command-line-args
    • unittest_magicmock_GPT
    • unittest_mock
    • unittest_printing
    • unittest_subtest
    • useful-stuff
    • jupyter
    • poetry
  • ros
    • _ROS Cheat Sheet
    • Create New Workspace
    • Install ROS
    • Node Sample - Listener
    • Node Sample - Talker
    • Node Template
    • Setup
    • urdf
  • excel
    • excel-reference
  • windows
    • File System
    • WSL - Windows Subsystem for Linux
    • WSL
  • software_engineering
    • uncle_bob_lectures
      • Overview
      • Lesson 01 - Notes
  • web
    • Front End
    • Hugo
    • new_frontend_tools
  • sql
    • cheatsheet
Powered by GitBook
On this page
  1. python

Operations

Object and list unpacking

# Unpack list_a items into list_b
list_a = ['how', 'are', 'you']
list_b = ['hello', 'world', *list_a]

# Unpack dict_a into individual keys/values
dict_a = {
	'hello': 'world',
	'howare': 'you'
}
def func(hello: str, howare: str) -> None:
	print(hello)
	print(howare)
func(**dict_a)
# This will print the following:
world
you

List comprehension

# Single loop
list_a = [1, 2, 3, 4, 5]
list_b = [str(item) for item in list_a]
# Result will be:
["1", "2", "3", "4", "5"]

# Single loop with conditional
list_a = [1, 2, 3, 4, 5]
list_b = [str(item) if item > 2 else str(item + 3) for item in list_a]
# Result (list_b) will be:
["4", "5", "3", "4", "5"]

# Nested loop
list_a = [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6]]
list_b = [num + 1 for item in list_a for num in item]
# Result (list_b) will be:
[2, 3, 3, 4, 4, 5, 5, 6, 6, 7]

# Nuances about conditionals:
#
# If only "if" and no "else", it goes after the for loop(s).
list_b = [num + 1 for item in list_a for num in item if num > 2]
# If using "if" AND "else", it goes before the for loop(s).
list_b = [num + 1 if num > 2 else num for item in list_a for num in item]

Last updated 1 year ago